Once upon a time in a factory
httpFactory.factory('setFavorability', function($http){
return{
Like: function(id){
return $http.get('http://localhost:51265/Film/Like/' + id);
}
}
});
In the realm of controllers
$scope.thisLike = function(id){
setFavorability.Like(id).then((response) => {
$('.favorability-count').text(response.data);
})
}
All seems well and good. Except for my confusion around binding to element classes. I wish to go about it this way
<button
class="fa fa-thumbs-up like"
ng-click="favorability=thisLike(film.FilmId);"></button>
<div>
<span ng-class="{'low-rating':favorability < 0}"
class="favorability-count">{{ favorability }}</span>
</div>
<button
class="fa fa-thumbs-down dislike"
ng-click="favorability=thisDisLike(film.FilmId)"></button>
So, initialize a local variable in the markup and assign it from the returning method. However, I struggle to get the correct value.
I attempted this approach
$scope.thisLike = function(id){
let v = 0;
setFavorability.Like(id).then((response) => {
v = response.data;
})
return v;
}
Alas, the method returns prematurely without awaiting the value retrieval. Thus, I receive 0.