Tôi đang cố gắng sử dụng vật liệu góc autocomplete thành phần trong dự án angular2 của tôi. Tôi đã thêm vào sau mẫu của tôi.Không thể liên kết với 'formControl' vì nó không phải là tài sản được biết đến của 'đầu vào' - tài liệu góc 2 Tự động hoàn thành
<md-input-container>
<input mdInput placeholder="Category" [mdAutocomplete]="auto" [formControl]="stateCtrl">
</md-input-container>
<md-autocomplete #auto="mdAutocomplete">
<md-option *ngFor="let state of filteredStates | async" [value]="state">
{{ state }}
</md-option>
</md-autocomplete>
Sau đây là thành phần của tôi.
import {Component, OnInit} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {FormControl} from "@angular/forms";
@Component({
templateUrl: './edit_item.component.html',
styleUrls: ['./edit_item.component.scss']
})
export class EditItemComponent implements OnInit {
stateCtrl: FormControl;
states = [....some data....];
constructor(private route: ActivatedRoute, private router: Router) {
this.stateCtrl = new FormControl();
this.filteredStates = this.stateCtrl.valueChanges.startWith(null).map(name => this.filterStates(name));
}
ngOnInit(): void {
}
filterStates(val: string) {
return val ? this.states.filter((s) => new RegExp(val, 'gi').test(s)) : this.states;
}
}
Tôi nhận được lỗi sau. Có vẻ như chỉ thị formControl
không được tìm thấy.
Không thể liên kết với 'formControl' vì nó không phải là một tài sản của 'đầu vào'
vấn đề ở đây là gì được biết đến?
một bình luận cho câu trả lời Pengyy của: Trong khi sử dụng 'formControl', bạn phải nhập khẩu ' ReactiveFormsModule' vào ** mô-đun của bạn **, không phải * rootModule *. Chỉ trong trường hợp bạn sử dụng 'FormControl' trong các mô-đun tính năng của bạn. –
Tôi có trường hợp tương tự và có nhập khẩu cho ReactiveFormsModule trong tính năng của tôi. Sự khác biệt duy nhất là tôi muốn liên kết với 'formControlName' thay vì 'formControl'. Thông báo có cùng cấu trúc –