无法绑定到 'formControl',因为它不是 'input' 的已知 属性 - Angular2 Material 自动完成问题
Can't bind to 'formControl' since it isn't a known property of 'input' - Angular2 Material Autocomplete issue
我正在尝试在我的 Angular 2 项目中使用 Angular Material Autocomplete 组件。我将以下内容添加到我的模板中。
<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>
以下是我的组件。
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;
}
}
我收到以下错误。似乎没有找到 formControl
指令。
Can't bind to 'formControl' since it isn't a known property of 'input'
这里有什么问题?
使用 formControl
时,您必须将 ReactiveFormsModule
导入 imports
数组。
示例:
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
@NgModule({
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
MaterialModule,
],
...
})
export class AppModule {}
忘记尝试破译示例 .ts - 正如其他人所说,它通常是不完整的。
只需单击此处圈出的 'pop-out' 图标,您将获得 fully working StackBlitz example。
您可以快速确认需要的模块:
注释掉 ReactiveFormsModule
的任何实例,果然您会得到错误:
Template parse errors:
Can't bind to 'formControl' since it isn't a known property of 'input'.
首先向您的模板添加一个常规的 matInput。假设您正在使用 ReactiveFormsModule 中的 formControl 指令来跟踪输入的值。
响应式表单提供了一种模型驱动的方法来处理值随时间变化的表单输入。本指南向您展示如何创建和更新简单的表单控件、如何在一个组中使用多个控件、验证表单值以及实现更高级的表单。
import { FormsModule, ReactiveFormsModule } from "@angular/forms"; //this to use ngModule
...
imports: [
BrowserModule,
AppRoutingModule,
HttpModule,
FormsModule,
RouterModule,
ReactiveFormsModule,
BrowserAnimationsModule,
MaterialModule],
从 9.1.4 版本开始,您只需导入 ReactiveFormsModule
发生这种情况的另一个原因:
您在 formControl
中使用的组件未在导入 ReactiveFormsModule
的模块中声明。
因此检查声明抛出此错误的组件的模块。
在 angular 12 中,matautocompleteModule 的导入路径已更改,它
解决了我的问题...现在看起来像这样
嗯,对我有用的是在 @component({}) 的模板`` 中构建表单,例如--
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-contact-form',
template:`
<form class="validation-form" validate method="createUserWithEmailAndPassword">
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="firstName">First name</label>
<input type="text" [formControl]="firstName" id="firstName" placeholder="Please enter your first name" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-6 mb-3">
<label for="lastName">Last name</label>
<input type="text" [formControl]="lastName" id="lastName" placeholder="Please enter your last name" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="email">Email address</label>
<input type="email" [formControl]="email" id="email" aria-describedby="emailHelp" placeholder="Please enter your last name" required>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
<div class="col-md-6 mb-3">
<label for="password">Password</label>
<input type="password" [formControl]="password" id="password" placeholder="Please enter your password" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-3 mb-3">
<label for="zip">Zip</label>
<input type="text" [formControl]="zip" id="zip" required>
<div class="invalid-feedback">
Please provide a valid zip.
</div>
</div>
</div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<div class="invalid-feedback">
You must agree before submitting.
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit form</button>
</form>`,
templateUrl: './contact-form.component.html',
styleUrls: ['./contact-form.component.scss']
})
export class ContactFormComponent implements OnInit {
firstName = new FormControl('');
lastName = new FormControl('');
email = new FormControl('');
password = new FormControl('');
constructor() { }
ngOnInit(): void {
}
}
这不再显示错误。如果错误仍然存在,那么这可能对你有用
我正在尝试在我的 Angular 2 项目中使用 Angular Material Autocomplete 组件。我将以下内容添加到我的模板中。
<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>
以下是我的组件。
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;
}
}
我收到以下错误。似乎没有找到 formControl
指令。
Can't bind to 'formControl' since it isn't a known property of 'input'
这里有什么问题?
使用 formControl
时,您必须将 ReactiveFormsModule
导入 imports
数组。
示例:
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
@NgModule({
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
MaterialModule,
],
...
})
export class AppModule {}
忘记尝试破译示例 .ts - 正如其他人所说,它通常是不完整的。
只需单击此处圈出的 'pop-out' 图标,您将获得 fully working StackBlitz example。
您可以快速确认需要的模块:
注释掉 ReactiveFormsModule
的任何实例,果然您会得到错误:
Template parse errors:
Can't bind to 'formControl' since it isn't a known property of 'input'.
首先向您的模板添加一个常规的 matInput。假设您正在使用 ReactiveFormsModule 中的 formControl 指令来跟踪输入的值。
响应式表单提供了一种模型驱动的方法来处理值随时间变化的表单输入。本指南向您展示如何创建和更新简单的表单控件、如何在一个组中使用多个控件、验证表单值以及实现更高级的表单。
import { FormsModule, ReactiveFormsModule } from "@angular/forms"; //this to use ngModule
...
imports: [
BrowserModule,
AppRoutingModule,
HttpModule,
FormsModule,
RouterModule,
ReactiveFormsModule,
BrowserAnimationsModule,
MaterialModule],
从 9.1.4 版本开始,您只需导入 ReactiveFormsModule
发生这种情况的另一个原因:
您在 formControl
中使用的组件未在导入 ReactiveFormsModule
的模块中声明。
因此检查声明抛出此错误的组件的模块。
在 angular 12 中,matautocompleteModule 的导入路径已更改,它
解决了我的问题...现在看起来像这样
嗯,对我有用的是在 @component({}) 的模板`` 中构建表单,例如--
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-contact-form',
template:`
<form class="validation-form" validate method="createUserWithEmailAndPassword">
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="firstName">First name</label>
<input type="text" [formControl]="firstName" id="firstName" placeholder="Please enter your first name" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-6 mb-3">
<label for="lastName">Last name</label>
<input type="text" [formControl]="lastName" id="lastName" placeholder="Please enter your last name" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="email">Email address</label>
<input type="email" [formControl]="email" id="email" aria-describedby="emailHelp" placeholder="Please enter your last name" required>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
<div class="col-md-6 mb-3">
<label for="password">Password</label>
<input type="password" [formControl]="password" id="password" placeholder="Please enter your password" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-3 mb-3">
<label for="zip">Zip</label>
<input type="text" [formControl]="zip" id="zip" required>
<div class="invalid-feedback">
Please provide a valid zip.
</div>
</div>
</div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<div class="invalid-feedback">
You must agree before submitting.
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit form</button>
</form>`,
templateUrl: './contact-form.component.html',
styleUrls: ['./contact-form.component.scss']
})
export class ContactFormComponent implements OnInit {
firstName = new FormControl('');
lastName = new FormControl('');
email = new FormControl('');
password = new FormControl('');
constructor() { }
ngOnInit(): void {
}
}
这不再显示错误。如果错误仍然存在,那么这可能对你有用