Michael Michael - 4 months ago 19
AngularJS Question

Show returned result from controller in template

I call function from HTML and I want to display returned result.

Here is html code:

<td ng-init="'damages = list.getDamageEvents(soData.damagesId)'">
{{damages}}
</td>


here is function in controller:

self.getDamageEvents = function (reviewsId) {
var t;
return $http.get(config.baseUrl + "api/DamageEvent/GetEventsByReviewsId/" + reviewsId).then(function (result) {
t=result.data;
});
return t
}


the getDamageEvents() function return some string.

But in template view I don't see any returned result.


EDIT: I need to access getDamageEvents function as isolate scope.


Any idea why I can't see returned result?

Answer

$http.get return a promise, so you can't return a result at the end of your function getDamageEvents, instead you should affect your result to a variable called damages in the success callback like this :

self.getDamageEvents = function (reviewsId) {
   $http.get(config.baseUrl + "api/DamageEvent/GetEventsByReviewsId/" + reviewsId)
   .then(function (result) {
     self.damages = result.data;
   });
}

Then you can access the damages variable in your view

Comments