我想要识别表格中的按钮,所以如果单击它们中的一个,我可以改变样式。这是我的工作表:从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。我已更新。 :) –