我正在嘗試使用formControl
中的disabled
屬性。當我把它的模板,它的工作原理:無效表單 - 禁用屬性
<md-input formControlName="id" placeholder="ID" [disabled]="true"></md-input>
但瀏覽器提醒我:
它看起來像你使用一個反應形式 指令禁用屬性。如果在組件類中設置此控件時將禁用屬性設置爲true ,那麼將禁用屬性實際上設置爲DOM中的 您。我們建議使用這種方法來避免「檢查後更改」錯誤。
Example: form = new FormGroup({ first: new FormControl({value: 'Nancy', disabled: true}, Validators.required), last: new FormControl('Drew', Validators.required) });
所以我把它放在FormControl
,並從模板中刪除:
constructor(private itemsService: ItemsService) {
this._items = [];
this.myForm = new FormGroup({
id: new FormControl({value: '', disabled: true}, Validators.required),
title: new FormControl(),
description: new FormControl()
});
this.id = this.myForm.controls['id'];
this.title = this.myForm.controls['title'];
this.description = this.myForm.controls['description'];
this.id.patchValue(this._items.length);
}
但它不工作(它不禁止輸入)。問題是什麼?
這似乎只是正常工作,具有角2的當前版本: http://plnkr.co/edit/CQQtkYC9D5EoH0sAlNCV ?p = preview – silentsod
我正在使用最新的角度cli項目進行測試 – FacundoGFlores
您正在使用@ angular/material,因此,根據他們的github問題:https://github.com/angular/material2/issues/1171 它是尚未支持,並且它們處於alpha模式,因此您不能指望它功能完整。 – silentsod