I have a Facebook style like system in my app and I want to make the like button change colour when the user clicks. This is to be done by adding an .active class, but I cant figure out how to get just the one item in my ng-repeat to have thr active class.
Heres my view:
<div class="list card" ng-repeat="data in array">
<div>
<a ng-click="favourite(data.ID)" class="tab-item" >
<i ng-class="{'active': favourited}" class="icon ion-thumbsup" ></i>
Favourite
</a>
</div>
</div>
The ng-click sends the request to the server to store in the database, and the ng-class changes the class to active when the "favourite" variable from the controller changes to true once this request is sucesfull:
$scope.favourite = function(dataID){
$favourite.favourite(dataID).then(function(data){
$scope.favourited = true;
});
}
This causes all of the favourite buttons to become active, so I just dont know how to make just the current button active.
Thanks in advance
This is because you are using $scope.favourited as there is only single copy of this variable for all ng-repeats, hence it updates for all. So try to use some variable for individual record as per your requirement as you only want to mark a single record as favourite at a time.
Replace
ng-class="{'active': favourited}"
with ng-class="{'active': data.favourited}"
and
$scope.favourited = true;
with data.favourited = true;