Angularjs : Scope Issue In $http Success Callback
I'm using PHP to get data into my factory, which shows correctly in the success callback function within the controller. However, even after assigning the returned data to $scope.c
Solution 1:
$http functions happen asynchronously. Thus, calling console.log after the customersFactory.getAllData
will always be empty.
console.log($scope.customers); // Object with correct data
is actually happening AFTER
console.log($scope.customers); // empty []
You can trust the success callback to set $scope.customers
correctly, you just need your site to understand that it will happen later. If you NEED scope.customers to be populated before the view loads, consider using a resolve on the controller.
Solution 2:
Isn't
console.log($scope.customers); // empty []
executing before
console.log($scope.customers); // Object with correct data
?
The second one is in a success() callback so it could execute much later than the first one.
Post a Comment for "Angularjs : Scope Issue In $http Success Callback"