angularjs - $scope variable has no content -



angularjs - $scope variable has no content -

i had controller , function getdata shown. problem $scope.results outside loop has no content. while within sec http.get request, has content.

appcontrollers.controller('myasellerorderctrl', ['$scope', '$rootscope', 'order', '$http', function($scope, $rootscope, order, $http) { $scope.results = []; $scope.getdata = function() { $http.get('api/orders/business/?user_id=' + $rootscope.user.user_id).success(function(data){ (var = 0; < data.length; i++) { $http.get('api/orders/seller/?business_id=' + data[i].business_id).success(function(data1){ // console.log(data1); $scope.results[i] = data1; }); } console.log($scope.results); }); }; $scope.getdata(); }]);

explanation

// step 1: first request $http.get('api/orders/business/?user_id=' + $rootscope.user.user_id).success(function(data){ // step 2 : loop started (var = 0; < data.length; i++) { // step 3 : nail request. not block operation because run asynchronously. // not wait previous request completed. $http.get('api/orders/seller/?business_id=' + data[i].business_id).success(function(data1){ // console.log(data1); $scope.results[i] = data1; }); } // step 4: doesn't wait step 3 finish because [step 3 ] run asynchronously. // lin run after loop complete. console.log($scope.results); // nil here step 3 still running });

reference

https://developer.mozilla.org/en-us/docs/web/api/xmlhttprequest/synchronous_and_asynchronous_requests how $http synchronous phone call angularjs

angularjs angularjs-scope

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -