使用ng-repeat动态创建对象,希望默认选中复选框
dynamically created object with ng-repeat - want to default that checkboxes are selected
所以在StackOverflow中有大量关于ng-repeat和复选框的引用。然而,我已经试过了,仍然不能得到这个工作。
我想做的是,在点击{{address.bookName}}
时,默认选择通过ng-repeat填充的所有联系人。我试着添加ng-checked = true,虽然它表面上勾选了复选框,但它没有创建receivers.selected[contact.firstName]
的属性和{{contact.contact}}
的相应值。
<ul data-ng-repeat='address in addressBook'>
<li>
<a data-ng-click='showContacts = !showContacts'>{{address.bookName}}</a>
<ul>
<li data-ng-show='showContacts' data-ng-repeat='contact in address.addresses'>
<label>
<input
type='checkbox'
ng-model='receivers.selected[contact.firstName]'
name='selectedReceivers[]'
ng-true-value = '{{contact.contact}}'
ng-false-value='null'/>
{{contact.firstName}} {{contact.lastName}} - {{contact.contact}}
</label>
</li>
</ul>
</li>
</ul>
在我的控制器中我有一个对象:
$scope.receivers = {
selected: {}
};
总结:点击{{address.bookName}}
,我想要地址中的所有联系人。要选择的地址,因此要用所有联系人的firstName和contact填充$scope.receivers
。
data-ng-show='showContacts == $parent.$index'
,这将显示所单击的book-name的特定地址。
可以使复选框被ng-checked= true
选中。
在您的情况下,您可以通过使用$index (To get current / inner loop index)
和$parent.$index (To get parent loop index)
动态应用ng-model。
<input type='checkbox'
ng-model="receivers.checked[$parent.$index][$index]"
ng-checked="true"
ng-true-value="{{contact}}"
ng-false-value="null" />
最后,使用angular.copy(element)
复制作用域变量。
//Copy the scope variable
$scope.receivers.checked[index] = angular.copy($scope.addressBook[index].addresses);
function LoginController($scope) {
//Declare ng-model
$scope.receivers = {
selected: {},
checked: {}
};
$scope.showSelected = function(parentIndex, index)
{
//Show hide particular contacts
$scope.showContacts = index;
//Copy the scope variable
$scope.receivers.checked[index] = angular.copy($scope.addressBook[index].addresses);
}
$scope.addressBook = [{
"_id": 1,
'bookName': 'Family',
'companyID': 1234,
'creator': 1234,
'addresses': [{
'contact': '1234567890',
'firstName': 'Bob',
'lastName': 'Smith'
}, {
'contact': '1234567890',
'firstName': 'Amy',
'lastName': 'Smith'
}, {
'contact': '1234567890',
'firstName': 'John',
'lastName': 'Smith'
}]
}, {
'_id': 2,
'bookName': 'Friends',
'companyID': 1234,
'creator': 1234,
'addresses': [{
'contact': '1234567890',
'firstName': 'Bruce',
'lastName': 'Wayne'
}, {
'contact': '1234567890',
'firstName': 'Clark',
'lastName': 'Kent'
}, {
'contact': '1234567890',
'firstName': 'Peter',
'lastName': 'Parker'
}]
}];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="LoginController">
<h5 class="form-title">Select contacts:</h5>
<ul data-ng-repeat='address in addressBook'>
<li>
<a data-ng-click='showSelected($parent.$index, $index)'>{{address.bookName}}</a>
<ul>
<li data-ng-show='showContacts == $parent.$index' data-ng-repeat='contact in address.addresses'>
<label>
<input type='checkbox'
ng-model="receivers.checked[$parent.$index][$index]"
ng-checked="true"
ng-true-value="{{contact}}"
ng-false-value="null" /> {{contact.firstName}} {{contact.lastName}} - {{contact.contact}}
</label>
</li>
</ul>
</li>
</ul>
<h5 class="text-bold">Send to: {{receivers.checked}}</h5>
</div>
相关文章:
- 如果用户未选中复选框或选择按钮,如何为复选框或单选按钮设置一些默认值
- JS:如果select是默认值,则禁用复选框
- 获取从默认设置更改的所有复选框的 id
- “角度表单”复选框默认值
- 在自动生成的表单上设置默认选中的复选框
- 复选框默认值“选中”,使用 jquery 1.9.1 给出未定义的值
- 使用jQuery切换根据选中的复选框隐藏/显示表单提交按钮 - 如何设置默认关闭的提交按钮
- 如何通过默认angularjs选中一个复选框
- 如何使用AngularJS设置数据库中复选框中选中的默认值
- 将复选框的默认值绑定到角度模型,其中true和amp;false表示为'是'&'没有'
- 如何在默认情况下始终取消选中复选框
- 如何将复选框设为默认
- 如何使用angularjs中的自定义指令,使用Unform jQuery插件设置浏览器默认复选框的样式
- 复选框默认图标不会使用 jQuery 更新
- 使用Jquery给复选框默认的复选值
- Jstree:当我重新加载树时,我不能将所有的复选框重置为默认取消选中
- Jquery默认禁用复选框
- 更改后返回默认复选框
- jQuery复选框启用/禁用文本输入和添加/删除默认值
- 选择默认复选框