[英]Angular disable input if checkbox is checked
I have one input and one checkbox and I want to disable the input if checkbox is checked.我有一个输入和一个复选框,如果选中复选框,我想禁用输入。 Any suggestions?
有什么建议么?
<input formControlName="checkbox" type="checkbox">
<input formControlName="input" type="number">
You can use reactive forms valueChanges
observable on your checkbox control to achieve this.您可以使用复选框控件上可观察到的反应式 forms
valueChanges
来实现此目的。
@Component({...})
export class MyComponent implements OnInit {
// Your form
form:FormGroup = this.fb.group({checkbox: false, input: ''});
constructor(private fb:FormBuilder){}
ngOnInit() {
// Subscribe to value changes - this will trigger whenever the value of your checkbox is changed (through click, or programmatically)
this.form.get('checkbox').valueChanges
.subscribe(value => {
if (value) {
// disable the input when new value is true
this.form.get('input').disable();
} else {
// (re-)enable the input when new value is false
this.form.get('input').enable();
}
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.