view - AngularJS: looping ajax requests, fetch data to scope when done -
in controller, i'm calling mill fetches info api, in success function, info passed function loops through , calls mill each round in loop. info retrieved sec mill mashed info first mill in new object, each round in loop, , pushed array.
when add together info $scope, view beingness updated info beingness fetched, can see items in view added 1 1 , not in bunch.
i need sort info in $scope before hits view.
is there way when actions finished? – when info fetched.
app.controller('myctrl', function($scope, firstfactory, secondfactory) { var objarray = []; function dostuff() { firstfactory.getdata().success(function(data) { domore(data); }); } function domore(data) { $.each(data, function(key, value) { secondfactory.getdata(value).success(function(result) { var obj = { test: result.test, test2: value.test }; objarray.push(obj); }); }); $scope.data = objarray; } });
for sec phone call can utilize $q.all. resolved when calls complete
function domore(data) { var promises = []; $.each(data, function (key, value) { promises.push(secondfactory.getdata(value)); }); $q.all(promises).then(function (responsearray) { $.each(responsearray, function (result) { var obj = { test: result.test }; objarray.push(obj); }); $scope.data = objarray; }); }
angularjs view angularjs-scope factory
No comments:
Post a Comment