{{article.title}}
{{article.content}}
How can I ensure that the complete()
function will run regardless of the outcome of the $http call using the promise API provided with Angular.js?
$
If you don't care if the request is successful or not, then you can pass the same callback to success
and error
...
var cb = function(response){
// do something
};
$http.post(submitUrl, $scope.data).success(cb).error(cb);
// OR
$http.post(submitUrl, $scope.data).then(cb, cb);
But be aware that success
and error
callbacks have a different signature than the then
callbacks.
Also, promises are recognized by the templating engine in angular, which means that in templates you can treat promises attached to a scope as if they were the resulting values.
This means that you can do this:
Controller:
$scope.article = $http.get('/articles/' + articleId);
Template:
{{article.title}}
{{article.content}}
And the view will update when the $http.get
promise has been resolved.