5
votes

i ran completly out of ideas. I want to user Reactive Forms Module, so i imported it in app.module.ts like

import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    ...
  ],
  imports: [
    ...
    ReactiveFormsModule,
    ...
  ],
  providers: [],
  bootstrap: [AppComponent]
})

In my Component i defined:

import { Component, OnInit} from "@angular/core";

import { FormControl, FormGroup } from '@angular/forms';

@Component({
    ...
})

export class SearchComponent implements OnInit{
    //Variablen
    form: FormGroup;
    
    //Konstruktor
    constructor(){}

    //Methoden
    ngOnInit(){
        this.form = new FormGroup({
            'title': new FormControl(null)
        });
    }

    showValue(){
        console.log(this.form.get('title'));
    }
}

Compiling works well, but when displaying it it crashes with the error below shown in the Browser Console: "core.js:6156 ERROR Error: NG0201: No provider for NgControl found in NodeInjector."

Does anybody of you has an idea what went wrong?

I would really appreciate any hint.

Thanks a lot!

4
As I know it is not recomend to use new FormGroup(...). Inject an instance of the FormBuilder and than use this.fb.group({ control: new FormControl(null)}). Maybee that can help you.Tr1monster

4 Answers

2
votes

To make this work you'll have to import the ReactiveFormsModule in your @NgModule which is the ViewsModule as your question suggests. As FormControl is exposed as a part of ReactiveFormsModule and NOT the FormsModule.

import { ReactiveFormsModule, ... } from '@angular/forms';

@NgModule({
  imports: [..., ReactiveFormsModule, ...],
  ...
})
export class ViewsModule {...}
1
votes

import FormsModule as well in app.module.ts

1
votes

Just found my (same) mistake, you have to import ReactiveFormsModule in "local" module... In your case it must be "search-component.module.ts"

1
votes

You can try to add *ngIf="!isLoading" in your form container in the html. In de .ts file add isLoading = true; in top of var declarations and then after your form is created, isLoading = false; Regards.