gpt4 book ai didi

javascript - Angular: "exportAs"设置为 "ngForm"时没有指令错误

转载 作者:行者123 更新时间:2023-11-30 08:22:51 25 4
gpt4 key购买 nike

我已尝试导入 FormsModule 和 NgForm 模块以及将 FormsModule 添加到导入数组。

下面是我的代码:

//our root app component
import { Component, NgModule, VERSION } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {FormsModule, NgForm} from '@angular/forms';

@Component({
selector: 'my-app',
template: `
<form #searchForm="ngForm">
<input type="text" required [(ngModel)]="model.search" ngControl="search" #inputSearch="ngForm">
<p class="error" [hidden]="inputSearch.valid"> This input is required</p>


</form>

`,
styles: [`
.error {
color: red;
font-size: 11px;
}
`]
})
export class App {
public model = {
search: ""
}

constructor() {

}
}

@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [App],
bootstrap: [App],
})
export class AppModule {}

下面是错误打印输出:

runtime.9ff156e16d788666a54a.js:16 Error: Template parse errors: There is no directive with "exportAs" set to "ngForm" (" ]#searchForm="ngForm"> ]#inputSearch="ngForm"> This input is required

"): ng:///AppModule/App.html@2:76 Can't bind to 'ngModel' since it isn't a known property of 'input'. (" ][(ngModel)]="model.search" ngControl="search" #inputSearch="ngForm"> https://run.plnkr.co/rhpwnL6UIQwCFOKZ/src/main.js Loading https://run.plnkr.co/rhpwnL6UIQwCFOKZ/src/main.js f @ runtime.9ff156e16d788666a54a.js:16

最佳答案

错误是由这一行引起的:

#inputSearch="ngForm"

这是正确的行:

#inputSearch="ngModel"

这是工作示例。当您在表单标签中使用 ngModel 时,您还需要为“name”属性提供值。

  <form #searchForm="ngForm">
<input type="text" required name="search" [(ngModel)]="model.search" #inputSearch="ngModel">
<p class="error" [hidden]="inputSearch.valid"> This input is required</p>
</form>

关于javascript - Angular: "exportAs"设置为 "ngForm"时没有指令错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50788790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com