angularjs - Order of execution of $http callbacks -


It looks like the factory's performance execution priority is highest, so that the callback does not have any data to deal with. What is the best way to do this work?

I found this type of factory

  app.factory ('jsonservice', function ($ http) {return}: function (callback) {$ http.get ( 'Data / Districts .Jason'). Success (callback);}, getLocations: function (path, callback) {$ http.get ('data /' + path + '.json') .vivet (callback);}} ;});   

and the controller

  var app = angular. Module ('sandbox', []); app.controller ('sandboxCtrl', function ($ radius, jsonService) {// it works with a $ scope.init1 = () {jsonService.getDistults (function) {$ scope.districts = data; $ scope. currentDistrict = $ scope.districts [0] .name; jsonService.getLocations ($ scope.currentDistrict, function (data) {$ scope.locations1 = data;})});}; $ scope.init1 (); // A $ scope.init2 = function is not () {jsonService.getDistricts (function) {$ scope.districts = data; $ scope.currentDistrict = $ scope.districts [0] .name;}) jsonService.getLocations ($ Scope .currentDistrict, function (data) {$ scope.locations1 = data;});}; $ scope.init2 ();});   

is working here

$ q () You should read on.

The condition of a race due to the asynchronous nature of the http call is, please review the updated code below, which successfully (successfully) using the promise to handle your call in succession Example shows.

So thanks to the power of the promise without your callback this second service method on the success of your first call.

  jsonService.getDistults () .success (function (data) {$ scope.districts = data; $ scope.currentDistrict = $ scope.distults [0] .name; jsonService.getLocations ($ Scope.currentDistrict) .success (function (location data) {$ scope.locations = locationData;})}); Promise clear: Use of the original promise to use  then  to handle responses and promises  $ http    additional methods ( Combine success ,  error ), which unpack your data from the feedback object, which you will need to handle if your just  then .   

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -