AngularJS - How to handle promises correctly -
i have setup angularjs resource:
app.factory('griddata', function($resource) { var csrf = 'asdf'; return $resource('/my/url/:expid', {expid:'@id'},{ get: {method:'get'}, query: {method:'get', isarray:true}, save:{method:'post', headers: {'x-csrftoken' : csrf }} }); }); i'm injecting griddata resource controller , using fetch data server:
griddata.query(function(result) { console.log("result is"); console.log(result); $scope.data = result; }); this approach works fine using angularjs 1.3. console.log(result) statement prints actual data received.
now tried downgrade angularjs 1.2.11 , i'm having issue since console.log(result) prints out promise, shown in picture. consequently $scope.data set promise, need $scope.data set when actual data available.
this because in version between 1.3 , 1.2 promises have been introduced angular. idea how handle situation / correctly deal promises?
check out section titled "the promise api" in the docs on $q service. can checkout this video on angular promises.
the main method need know .then. here's how refactor code method:
griddata.query(function(promise) { promise.then(function (result) { console.log("result is"); console.log(result); $scope.data = result; }); }); this example cleaner, works illustrative purposes. hope helps.
Comments
Post a Comment