角度错误突然出现错误:NodeInjector:NOT_FOUND [ControlContainer] [英] Getting error suddenly in Angular Error: NodeInjector: NOT_FOUND [ControlContainer]

查看:1109
本文介绍了角度错误突然出现错误:NodeInjector:NOT_FOUND [ControlContainer]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

core.js:5873错误错误:NodeInjector:NOT_FOUND [ControlContainer]. 有时,当我重新启动项目时,它可以完美运行. app.component.html中只有更改:

core.js:5873 ERROR Error: NodeInjector: NOT_FOUND [ControlContainer]. Sometime when i restart the project it runs perfectly. there are only changes in app.component.html :

 <div class="container">
  <div class="row">
    <div class="col-md-4">
      <form action="">

        <div class="form-group">
          <label for="">Username</label>
          <input type="text" name="username" class="form-control" />
        </div>
        <div class="form-group">
          <label>Password</label>
          <input type="password" class="form-control">
        </div>
        <div class="form-group">
          <label>Confirm Password</label>
          <input type="password" class="form-control">
        </div>
        <div>
          <button type="submit" class="btn btn-primary btn-block">Register</button>
        </div>
      </form>

    </div>
  </div>
</div>

推荐答案

app.module.ts中,在imports部分中添加ReactiveFormsModule.请记住将其导入为顶部的import { ReactiveFormsModule} from '@angular/forms.

In app.module.ts I add ReactiveFormsModule in my imports section. Remember to import it at the top as: import { ReactiveFormsModule} from '@angular/forms.

在您的app.component.ts中,您必须定义FormGroup实例并通过ngOnInit方法对其进行初始化/注册,如下所示:

In your app.component.ts you have to define FormGroup instance and initialize/register it via ngOnInit method as below:

    import { FormGroup, FormControl } from '@angular/forms'; //imports
.......................................................................
    myForm:FormGroup;  
    ngOnInit(){
         this.myForm = new FormGroup({          
               'name':new FormControl(null), //note, can have up to 3 Constructor Params: default value, validators, AsyncValidators
               'email':new FormControl(null,Validators.email)

          })
    }

然后将表单绑定到FormGroup实例myForm:

Then bind form to the FormGroup instance myForm:

<form [formGroup]="myForm">

请注意,nameemail是需要使用formControlName绑定的形式的控件:

Note that name and email are controls in the form that needs binding using formControlName :

<input type="text" name="name" formControlName="name">

这篇关于角度错误突然出现错误:NodeInjector:NOT_FOUND [ControlContainer]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆