Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Error: NodeInjector: NOT_FOUND [ControlContainer]

core.js:5873 ERROR Error: NodeInjector: NOT_FOUND [ControlContainer]

Sometimes 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>
like image 814
sanjay Avatar asked Feb 29 '20 06:02

sanjay


3 Answers

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

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)

          })
    }

Then bind form to the FormGroup instance myForm:

<form [formGroup]="myForm">

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

<input type="text" name="name" formControlName="name">
like image 196
Kenneth mwangi Avatar answered Nov 17 '22 11:11

Kenneth mwangi


you have to import both, import { FormsModule } from '@angular/forms'; import { ReactiveFormsModule } from '@angular/forms'; into your corresponding module.ts file. that worked for me.

like image 25
Theja Wijesiriwardane Avatar answered Nov 17 '22 12:11

Theja Wijesiriwardane


In your code formGroup is missing try this: <form [formGroup] = "">

like image 8
Ahmed Gabal Avatar answered Nov 17 '22 10:11

Ahmed Gabal