I am creating divs based on ng-repeat. I'm getting the data from a db. The data is like:
[
{
id:1,
name:item1,
category: [catA, catB, catC,]
},
{
id:2,
name:item2,
category: [catB]
},
{
id:3,
name:item3,
category: [catA, catB]
}
]
This data goes in a scope. So:
myApp.controller("myCtrl", function($scope, $http){
var cat = "catA";
$http.get("/getdata", function(response){
if(response.data){
$scope.items = response.data;
if(response.data.category==cat){ // Check if the var cat is present in the array. How to do this?
//set ng-class for the button in the div to 'active'. How to do this?
}
} else {
$scope.message = "Nothing returned.";
}
});
$scope.send = function(n){
$scope.isActive = !$scope.isActive;
}
});
I'm using ng-repeat :
<div ng-repeat="item in items" class="myclass">
{{item.name}}
<button class="myRight" ng-class="{'active': isActive}" ng-click="send(item.id)"> click </button>
</div>
Active Class:
.active { background: #FF735C; color:white;}
So, based on the var cat present in the array of category received in the data, the class of the button is decided. And if the button is clicked, the class toggles. I cannot use $index for the button to pass the var as if the data is filtered, the proper index is not rendered.
Am I right to use ng-class? Is there any other simpler way to achieve this?
Please help. Many thanks.
activeif the item hascatAas a category and notactiveonce the button is pressed?catAlikeitem2, the button in this div should be not have the active class.