There is a script. Need a little to redo it.
The essence of the script is to check if there is a service in the favorites array on which the user presses. Add / remove from favorites.

$scope.manageFavorService = function (serviceId) { var data = {};//sending an empty array to get response getService = serviceId; PortalApi.getFavorServices(data)//get an array with all favorites services .then(function(data) { function isFavorite(id)//looking if there already serviceId exist { return data.filter(function(data) { return data.service_id== id}); } var isFavorite = isFavorite(getService), data = {serviceId:serviceId}; if (isFavorite.length == 0) {//add favorite if in answer we got empty array PortalApi.addFavorServices(data); } else {//del favorite if in answer we got array with our service PortalApi.delFavorServices(data); } }); }; 

PortalApi.getFavorServices

 getFavorServices: function(data) { var defer = $q.defer(); watchResponse(function() { return services.one('receivers').one('getFavorServices').post('', { data: data }); }, function(response) { return defer.resolve(response); }, function(reason) { return defer.reject(reason); }); return defer.promise; }, 

I can not understand how to add it so that it would be possible to call the isFavorite check in the html code, for example, to change the button class. project on angularjs

I want to check there

 ng-class="{'glyphicon-heart' : isFavorite , 'glyphicon-heart-empty' : !isFavorite }" 

overall button

 <button ng-if="auth.user" ng-click="manageFavorService(service.id)" class="fa favorite-service" ng-class="{'fa-star' : isFavorite , 'fa-star-o' : !isFavorite }" uib-tooltip="{{!isFavorite ? 'SERVICES.FAVORITES.ADDTO' : 'SERVICES.FAVORITES.DELFROM' | translate}}" tooltip-placement="left"></button> 
  • add method implementation: PortalApi.getFavorServices - Grundy
  • 2
    like this var isFavorite = isFavorite better not to do - Grundy
  • one
    how to add it so that you can call the isFavorite check in the html code - there is no way that you have access in the markup, the variable must be inside $ scope, and you now have a local variable - Grundy
  • Well, if you do this $ scope.manageFavorService = function ($ scope, serviceId) {is it really not possible to finish it like that? - Maxim
  • This option worked ng-class = "isFavorite? 'fa-star': 'fa-star-o'" $ scope.isFavorite = getFavorList (getService), but when clicked, it changes all elements on the page and only 1 time. why? - Maxim

0