【发布时间】:2017-10-04 15:06:05
【问题描述】:
我在 Angular 中使用了属性绑定来取消选择所有复选框,但它没有按预期工作。 我的预期:当我点击“清除”按钮选择一个或多个复选框后,它应该取消选择所有复选框。 这是我的plunker
isSelected is the boolean variable which i have used to set the 'checked' attribute of checkbox.
模板:
<div *ngFor="let item of items">
<input [checked]="isSelected" type="checkbox">{{item}}
</div>
<button (click)="onClear()">Clear All</button>
组件
private items = ['a', 'b', 'c', 'd'];
private isSelected = false;
constructor() {
}
onClear(){
this.isSelected = false;
}
【问题讨论】:
-
您不会像那样更改
checked元素属性值,您只是在更改类中的属性isSelected值。 -
要继续@onetwo12,您需要添加
([checked])括号使其成为双向绑定参考类似答案here -
@onetwo12 不是依赖于类的 'isSelected' 属性的 'checked' 属性吗?
-
问题是“isSelected”永远不会改变(总是假的)。 - 你可以先探测 if equals isSelected=true - 所以,Angular 不会刷新。无论如何,您必须有一些 items=[{title:'a',selected:true},{title:'b',selected:true},...]
标签: javascript angular ngfor property-binding