http - angularJS access properties of promised object in succes function -


i have problem can't head around. i'm using basic angular http.get returns promise:

gettask: function(id) {     var promise = $http.get('http://localhost:9000/get/tasks/'+id).success(function(data) {             return data;         });         return promise;     } tasksservice.gettask($stateparams.id).then(function(success){     $scope.task = success.data; }); 

the task has propperties want access, somehow returns undefined. if example do:

tasksservice.gettask($stateparams.id).then(function(success){     $scope.task = success.data;     console.log($scope.task.deadline); }); 

it's undefined. have idea how can access properties , how can share rest of controller?

ah, confusion of .success vs. .then strikes again. subtle (and, in opinion, unnecessary) difference angular introduced have spawn lot of issues.

the issue here .success returns original promise and, in respect, disregards data return. in other words, "branches off" , allows implement handler not affect rest of chain.

to fix, use .then:

return $http.get(url).then(function(response) {   return response.data; }); 

addendum:

to better understand, .success conceptually behaves follows:

promise.success = function(fn) {    promise.then(function(response) {      fn(response.data, response.status, response.headers, config);    });     // original promise returned, not .then promise    return promise; } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -