Reputation: 3393
I am trying to understand the difference between those two http calls in my factory:
One: function(code) {
return $http.get('api/team/' + code)
.then(function(resp) {
return resp.data;
});
}
}
And
Two: function(code) {
var promise = $http.get('api/team/' + code);
promise.then(function(resp) {
return resp.data;
});
return promise;
}
If I use Two
in resolve
in config
:
resolve: {
testt: ['$route','MyService',
function($route, MyService) {
return MyService.Two($route.current.params.code);
}]
}
Then I can see the data in my ng-view
. If I use One
, I dont see the data in ng-view
.
My controller:
.controller('TeamDetailsCtrl',
['MyService','testt',
function(MyService,testt) {
var self = this;
self.team = testt.data;
}]);
So, what is the difference? Best Regards
Upvotes: 0
Views: 82
Reputation: 670
One: When $http.get()
is done, it'll resolve and continue into the then
. This returns another promise, filled with the result of the then function result.data
. One returns the second promise.
Two: Returns the original promise from $http.get()
with the result result
, (in which you haven't transformed result into result.data). The correct syntax for two could be:
Two: function(code) {
var promise = $http.get('api/team/' + code);
var promise2 = promise.then(function(resp) {
return resp.data;
});
return promise2;
}
Upvotes: 2
Reputation: 4248
One
returns a promise, which will eventually return resp.data
.
If you use One
, resp.data === testt
in your controller. testt.data
doesn't work because resp.data.data
doesn't work.
If you want to use One
, change self.team = testt.data
to self.team = testt
.
Upvotes: 1