我有多个复选框和一个按钮,只有在选中至少一个复选框时才需要启用该按钮
<input type="checkbox">VALUE1
<input type="checkbox">VALUE2
<input type="checkbox">VALUE3
<input type="checkbox">VALUE4
<button>Proceed</button>
Run Code Online (Sandbox Code Playgroud)
如何使用Angular2实现这一目标.
PS:发现了类似的问题,但没有使用Angular2.
Mar*_*cok 43
一种方法是ngModel在每个复选框上使用,然后disabled通过检查每个复选框模型状态的方法控制按钮的属性:
@Component({
template: `
<label *ngFor="let cb of checkboxes">
<input type="checkbox" [(ngModel)]="cb.state">{{cb.label}}
</label>
<p><button [disabled]="buttonState()">button</button>
`
})
class App {
checkboxes = [{label: 'one'},{label: 'two'}];
constructor() {}
buttonState() {
return !this.checkboxes.some(_ => _.state);
}
}
Run Code Online (Sandbox Code Playgroud)
使用属性[禁用]:
<input type="checkbox" [(ng-model)]="disableButton1"> VALUE1
<input type="checkbox" [(ng-model)]="disableButton2"> VALUE1
<input type="checkbox" [(ng-model)]="disableButton3"> VALUE1
<input type="checkbox" [(ng-model)]="disableButton4"> VALUE1
<button type="button" [disabled]="disableButton || disableButton2">Submit</button>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68358 次 |
| 最近记录: |