Andurit
Andurit

Reputation: 5762

Is it possible to resolve AngularJS promises in a template?

in all documentation and tutorials for HTTP request i found that recomanded usage is something like:

var ax = {
    getCaseData: function() {
     api.cases.getCase(caseManager.data.id).then(function(res){
       // puting my response to $scope.something
       $scope.something = res.data;
     });
    },
}

I really don't feel like senior so please tell me if I am wrong but I thing many times you don't need to store something like this on scope, for example you want to just resolve promise once and display data (you are not doing anything else with it)

So I was thinking if is there an option to make a promise as:

var ax = {
    getCaseData: function() {
     return api.cases.getCase(caseManager.data.id);
    },
}

and after this call tempalte something like:

<li ng-repeat="ax.getCaseData()"></li>

Upvotes: 0

Views: 768

Answers (1)

Neozaru
Neozaru

Reputation: 1130

This was handled automatically in old version of AngularJS (<1.2), but was removed since then. Some posts state that the feature can be re-enabled manually by adding this line in your ".config" function :

$parseProvider.unwrapPromises(true);

But this is not advised as a solution. You are currently doing it the right way. If you have plenty of cases like this, you can probably create your own "promises wrapper" function, and use it from your template.

See : https://stackoverflow.com/a/19472065/1636977

Upvotes: 1

Related Questions