【发布时间】:2018-08-21 20:57:53
【问题描述】:
在我的 Angular 应用程序中,我正在遍历一个集合并使用 input type="radio" 显示记录。
<tr ng-repeat="account in vm.pagedAccounts.items"
ng-class="{ 'highlight': (account.rowIsSelected) }"
<td>
<input
ng-model="account.rowIsSelected"
value="{{account}}"
name="selectedAccount"
ng-checked="account.rowIsSelected"
ng-change="vm.selectAccount(account)"
type="radio">
</td>
在我的控制器中,我首先将所有帐户的 rowIsSelected 属性设置为 false。
response.data.results.forEach(function(account) {
account.rowIsSelected = false;
});
所以,我只是确保无论何时 account.rowIsSelected 设置为某个值,都选中它。
这工作正常。
但是,在selectAccount函数中,如果点击不同的帐户,我想remove the previous all highlights and highlight the current one。
vm.selectAccount = function (account) {
if (account.rowIsSelected) {
//First set all false
vm.pagedAccounts.items.forEach(function(account) {
account.rowIsSelected = false;
});
var selectedAccount = vm.pagedAccounts.items
.filter(function(x){
return x.id=== account.id;
});
//Then set only that accounts property to true
selectedAccount[0].rowIsSelected = true;
}
但如果我点击同一行两次,it is no longer checked. I want to keep it checked and highlighted。
怎么做? 我所做的一切看起来对吗? 请帮忙。
【问题讨论】:
-
不要在同一个元素上使用 ng-checked 和 ng-model。我认为您应该始终使用 ng-model。
-
@PetrAveryanov 那我怎样才能让单选按钮选中它?
-
Angular 在单选按钮方面很时髦。您应该使用
ngModel而不是ngChecked。 docs.angularjs.org/api/ng/input/input%5Bradio%5D
标签: javascript angularjs radio-button