emk*_*a26 3 checkbox angular2-ngmodel ngfor angular
我将通过选中的复选框(已迭代)传递给ngModel时遇到问题。
<label class="btn btn-outline-secondary"
*ngFor="let test of tests" >
<input type="checkbox">
</label>
Run Code Online (Sandbox Code Playgroud)
在ts中我有模型:
testData = <any>{};
this.tests = [{
id: 1, name: 'test1'
},
{
id: 2, name: 'test2'
},
{
id: 3, name: 'test3'
},
]
Run Code Online (Sandbox Code Playgroud)
我尝试使用ngModel和ngModelChange,但是在显示选中复选框时仍然有问题。我怎样才能做到这一点?
我建议您在模型中添加一个属性并将其绑定到模板中。
<label class="btn btn-outline-secondary" *ngFor="let test of tests" >
<input type="checkbox" [(ngModel)]="test.isChecked">
</label>
Run Code Online (Sandbox Code Playgroud)
this.tests = [{
id: 1, name: 'test1', isChecked: false
},
{
id: 2, name: 'test2', isChecked: true
},
{
id: 3, name: 'test3', isChecked: false
},
]
Run Code Online (Sandbox Code Playgroud)
使用 [(ngModel)]="test.name"
<label class="btn btn-outline-secondary" *ngFor="let test of tests" >
<input type="checkbox" [(ngModel)]="test.selected" > {{test.name}} - {{test.selected}}
</label>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7557 次 |
最近记录: |