在Angular 4中选中复选框时启用输入字段

时间:2018-04-25 18:52:44

标签: angular angular4-forms

默认情况下,输入字段被禁用,当我勾选复选框时,我可以输入数据。角度4的任何想法?

2 个答案:

答案 0 :(得分:1)

不确定我是否理解此处的问题,但请确保您的复选框未绑定已设置为false(或默认布尔值)的模型

如果您只是在选中复选框时尝试启用复选框,则可以执行以下操作:

export class AppComponent {
    isDisabled = true;
    triggerSomeEvent() {
        this.isDisabled = !this.isDisabled;
        return;
    }
}

和你的HTML:

<input type="checkbox" name="myChk" id="myChk" (change)="triggerSomeEvent()" />
<input type="text" name="myTxt" id="myTxt" value="" [disabled]="isDisabled" />

答案 1 :(得分:0)

 <input type="text" class="form-control" formControlName="invoices" maxlength="6">
 <input type="checkbox" (change)="autoGenerate($event.target.checked)">

autoGenerate(value) {
if (value == true) {
  const invoiceNo = Math.max.apply(Math, this.lstInvoice.map(function (o) { return o.invoices; }))
  this.invoicesForm.get('invoices').setValue(invoiceNo + 1);
  this.invoicesForm.get('invoices').disable();
}
else {
  this.invoicesForm.get('invoices').setValue(null);
  this.invoicesForm.get('invoices').enable();
}

}