Tôi đang cố gắng sử dụng disabled
thuộc tính từ a formControl
. Khi tôi đặt nó vào mẫu, nó hoạt động:
<md-input formControlName="id" placeholder="ID" [disabled]="true"></md-input>
Nhưng trình duyệt cảnh báo tôi:
Có vẻ như bạn đang sử dụng thuộc tính bị vô hiệu hóa với chỉ thị biểu mẫu phản ứng. Nếu bạn đặt bị vô hiệu hóa thành true khi thiết lập điều khiển này trong lớp thành phần của mình, thì thuộc tính bị vô hiệu hóa sẽ thực sự được đặt trong DOM cho bạn. Chúng tôi khuyên bạn nên sử dụng phương pháp này để tránh lỗi 'đã thay đổi sau khi đã kiểm tra'.
Example: form = new FormGroup({ first: new FormControl({value: 'Nancy', disabled: true}, Validators.required), last: new FormControl('Drew', Validators.required) });
Vì vậy, tôi đặt nó vào FormControl
và xóa khỏi mẫu:
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);
}
Nhưng nó không hoạt động (nó không vô hiệu hóa input
). Vấn đề là gì?