Error: Export of name 'ngModel' not found

佐手、 提交于 2021-01-06 03:33:51

问题


After building my angular project i get the error: Error: Export of name 'ngModel' not found!

I have my UI running in a docker container

not even sure where to look for this. Its working fine in dev. (ng serve)

Any ideas


回答1:


import FormsModule in your respective spec.ts file and app.module.ts if not

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports:[RouterTestingModule,HttpClientTestingModule,FormsModule],
      declarations: [ MyComponent ]
    })
    .compileComponents();
  }));



回答2:


I had the same error (though in dev), it turns out that I had not added the FormsModule module to the app.module.ts file. See below:

It is also stated here along with a few more problems and their corresponding solutions




回答3:


You need to import FormsModule in your .spec.ts and app.module.ts (if necessary) file, as per below:

import { FormsModule } from '@angular/forms';    <------ Add this code

describe('Testing Component', () => {
  ...
  ...

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ 
            FormsModule      <------ Add this code
        ]
    })
    .compileComponents();
  }));

  ...
  ...



回答4:


I had a template variable called ngModel. Not sure what affect this had, but removing seemed to fix it.




回答5:


Another possible issue: if you're using template form validation When you set #name="ngModel" on a form field without [(ngModel)] directive

E.g.

<input required [value]="hero.name" (change)="setHeroName($event)" #name="ngModel" >

<div *ngIf="name.invalid && (name.dirty || name.touched)"
  class="alert alert-danger">Error</div>

=> Error: Export of name 'ngModel' not found

Fixed by replacing

[value]="hero.name" (change)="setHeroName($event)"

by

[(ngModel)]="hero.name"


来源:https://stackoverflow.com/questions/57725780/error-export-of-name-ngmodel-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!