我正在基於ng-repeat創建div。我從數據庫中獲取數據。數據如下:複合角腳本
[
{
id:1,
name:item1,
category: [catA, catB, catC,]
},
{
id:2,
name:item2,
category: [catB]
},
{
id:3,
name:item3,
category: [catA, catB]
}
]
該數據在一個範圍內。所以:
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;
}
});
我使用NG-重複:
<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 { background: #FF735C; color:white;}
因此,基於該var cat
目前category
在接收到的數據在陣列中,按鈕的類別決定。如果點擊該按鈕,該類將切換。我無法使用$ index作爲按鈕來傳遞var,就好像數據被過濾一樣,正確的索引也不會被渲染。
我有權使用ng級嗎?有沒有其他更簡單的方法來實現這一目標?
請幫忙。非常感謝。
所以確認,按鈕應該是'active'如果該項目有'catA'作爲一個類別,而不是'active'一旦按下按鈕? – Ankh
是的。究竟。如果一個項目沒有像'item2'這樣的'catA',那麼這個div中的按鈕應該沒有活動類。 – Somename
如果點擊了一個活動按鈕,它會變成「不活動」並且無法再次點擊? – Ankh