我想要識別表格中的按鈕,所以如果單擊它們中的一個,我可以改變樣式。這是我的工作表:從ng-repeat中改變元素的樣式
這裏是代碼:
<table class="table">
<thead>
<tr>
<th>Allergi navn</th>
<th>Allergi verdi</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="items in $ctrl.allergen">
<td>
<b>
{{items.Allergennavn}}
</b>
</td>
<td id="{{$index}}">
<button
id="tableButton"
type="button"
class="btn-grey"
ng-repeat="item in $ctrl.allergenVerdiType"
ng-click="$ctrl.allergiAssigned($index, items, item)"
>
<b>
{{item.Allergenverdi}}
</b>
</button>
<hr>
</td>
</tr>
</tbody>
</table>
和JS:
ctrl.allergiAssigned = function($index, itemAllergen, itemAllergenType){
var index = $('button').index(this);
$(index, '#tableButton').css("background-color", "green");
}
我嘗試了好幾種方法來引用具體按鈕元素,使用這個,索引等。我還需要確定,對於每一行,只有一個被選中的按鈕。
我也嘗試通過{{$index}}
來獲取該行的唯一標識符,但jquery不支持該語法。
更新基於答案:
<table class="table">
<thead>
<tr>
<th>Allergi navn</th>
<th>Allergi verdi</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="items in $ctrl.allergen">
<td>
<b>
{{items.Allergennavn}}
</b>
</td>
<td id="{{$index}}">
<button id="tableButton" type="button"
class="btn-grey" ng-class="{activeBtn: item.active == true}"
ng-repeat="item in $ctrl.allergenVerdiType"
ng-click="$ctrl.select(items.type, item)">
{{item.AllergenverditypeKode}}</button>
<hr>
</td>
</tr>
</tbody>
</table>
ctrl.select = function(items, index) {
angular.forEach(items, function(item){
item.active=false;
});
index.active = true;
};
指數返回undefined
我updataed我的問題根據您的回答,但我仍然有JS的 –
@Immanuel納克級=「{activeBtn麻煩:item.active}「就足夠了。不需要再次檢查,像'item.active == true' –
是的,你是對的@KannanThangadurai。我已更新。 :) –