微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

没有找到带有 exportAs 'ngModel' 的指令角 12

如何解决没有找到带有 exportAs 'ngModel' 的指令角 12

我正在以 angular 创建表单验证,但出现错误

未找到带有 exportAs 'ngModel' 的指令。

我的代码

<form>
  <div class="form-group">
      <label for="firstname">First Name</label>
      <input type="text" id="name" name="name" required minlength="4" 
                     appForbiddenName="bob" ngModel #name="ngModel">
                     
      <div class="alert alert-danger" *ngIf>First name required</div>
  </div>
  <div class="form-group">
      <label for="comment">Comment</label>
      <textarea name="" id="comment" cols="30" rows="10" class="form-control"></textarea>
  </div>
  <button class="btn btn-primary">Submit</button>
</form>

image of editor

错误

     Error: src/app/app.component.html:6:60 - error NG8003: No directive found with exportAs 'ngModel'.
    
    6                      appForbiddenName="bob" ngModel #name="ngModel">
                                                                 ~~~~~~~
    
      src/app/app.component.ts:5:16
        5   templateUrl: './app.component.html',~~~~~~~~~~~~~~~~~~~~~~

组件AppComponent的模板出错。

解决方法

每当您遇到此类错误时,请确保您已在主模块中导入了 forms module

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule,ReactiveFormsModule } from '@angular/forms'; // <== add the imports!
 
import { AppComponent }  from './app.component';
 
@NgModule({
  imports: [
    BrowserModule,FormsModule,ReactiveFormsModule                       
  ],declarations: [
    AppComponent
    // other components here
  ],bootstrap: [ AppComponent ]
})
export class AppModule { }

查看here了解更多详情。

,

如果您使用 Angular 模板驱动的表单并希望使用 #name="ngModel" 您还需要在同一输入中使用 [(ngModel)]="mymodel" 指令,当然, 添加 import { FormsModule } from '@angular/forms';到您的 app.module.ts 和导入数组中,您需要添加 FormsModule。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。