javascript - Tôi có thể sử dụng [formControl] mà không cần [formGroup] không?
Tôi đang sử dụng một dropdown trong giao diện người dùng của mình, nhưng khi tôi sử dụng [formControl] tôi nhận được lỗi sau:
Không thể tìm thấy điều khiển với thuộc tính tên không xác định
Tôi đang sử dụng ReactiveFormsModule trong app.module.ts của mình.
Tôi đã tìm trên Google và nhận thấy một giải pháp là sử dụng [formGroup] trong div cha, nhưng tôi không chắc cách triển khai đúng vì tôi đang định nghĩa formControl từ bên trong một subscribe.
myComp.component.html
<div class="exceptional-status">
<select #exceptionalSelect id="excep-status-dropdown" [formControl]="select">
<option disabled value="default">--Exceptional statuses--</option>
<!-- other options here with *ngFor -->
</select>
</div>
myComp.component.ts
select: FormControl;
mySubscription() {
this.myService.myFunct.subscribe(res => {
this.select = new FormControl(res.status)
});
}
Câu trả lời #1
Vâng, bạn có thể sử dụng FormControlDirective mà không cần FormGroup.
Angular mong đợi FormControl được định nghĩa:
select = new FormControl();
mySubscription() {
this.myService.myFunct.subscribe(res => {
this.select.setValue(res.status)
});
}
