リアクティブフォーム - 無効な属性 質問する

リアクティブフォーム - 無効な属性 質問する

disabledの属性を使用しようとしていますformControl。テンプレートに配置すると、次のように動作します。

<md-input formControlName="id" placeholder="ID" [disabled]="true"></md-input>

しかし、ブラウザは警告を表示します:

リアクティブ フォーム ディレクティブで disabled 属性を使用しているようです。コンポーネント クラスでこのコントロールを設定するときに disabled を true に設定すると、disabled 属性が実際に 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);
}

しかし、動作しません( が無効になっていませんinput)。何が問題なのでしょうか?

ベストアンサー1

[attr.disabled]私は、プログラムによる enable()/disable() よりも、テンプレート駆動の方が優れていると思うので、このテンプレート駆動を使い続けています。

変化

<md-input formControlName="id" placeholder="ID" [disabled]="true"></md-input>

<md-input formControlName="id" placeholder="ID" [attr.disabled]="true"></md-input>

新しい素材を使用している場合は、md-inputに変更しますmat-input

おすすめ記事