0

Event after declaring:

  • FormsModule,
  • ReactiveFormsModule,

in the feature module error still showing

home.component.html:

<p>home works!</p>
<div>
  <form [formGroup]="MyForm" (submit)="send()">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" />
    <br />
    <button type="submit">Submit</button>
  </form>
</div>

home.component.ts:

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
  public MyForm!: FormGroup;
  constructor(private fb: FormBuilder) { }

  ngOnInit(): void {
    this.MyForm = this.fb.group({
      productName: ['', [Validators.required]],
    })
  }

  public send() {
    console.log("FORM SUBMITED")
    console.log(this.MyForm.value)
  }
}

home.module.ts

@NgModule({
  declarations: [
    HomeComponent
  ],
  imports: [
    CommonModule,
    HomeRoutingModule,
    ReactiveFormsModule,
    FormsModule
  ]
})
export class HomeModule { }

home-routing.module.ts

const routes: Routes = [
  {
    path: '',
    component: HomeComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class HomeRoutingModule { }

I have followed this Can't bind to 'formGroup' since it isn't a known property of 'form'

but still error NG8002: Can't bind to 'formGroup' since it isn't a known property of 'form'.

EDIT: if I move the form in app.html, app.ts, app.module there is no error. but in a feature module there is.

small repo: https://github.com/ga2tes/AngularFormTest

1 Answers1

1

Your issue is lazy loading the HomeRoutingModule you should be loading the HomeModule.

In app-routing.module.ts:

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('./home/home.module').then(m => m.HomeModule)
  }
];
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134