Home > database >  Can't bind to 'formGroup' since it isn't a known property of 'form', i
Can't bind to 'formGroup' since it isn't a known property of 'form', i

Time:06-16

i got an issue that was already addressed here.

Can't bind to 'formGroup' since it isn't a known property of 'form'.

I added as explain ReactiveFormsModule, FormsModules in the required modules by issue is not solved

view.html

  <form formGroup="siteForm" (ngSubmit)="createNewSite()">
    <div >
      <label>Nom du site</label>
      <input type="text" formControlName="siteName"  required>
    </div>
    ...
    <button type="submit" >Envoyer</button>
  </form>

site.module.ts

import {NgModule} from '@angular/core';
import {CreateComponent} from './create/create.component';
import {ReadComponent} from './read/read.component';
import {BrowserModule} from "@angular/platform-browser";
import {FormsModule, ReactiveFormsModule} from "@angular/forms";


@NgModule({
  declarations: [
    CreateComponent,
    ReadComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule
  ]
})
export class SiteModule {
}

create.component.ts

import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";

@Component({
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.scss']
})
export class CreateComponent implements OnInit {

  siteForm!: FormGroup;

  constructor(private formBuilder: FormBuilder) {
  }

  ngOnInit(): void {
    this.siteForm = this.formBuilder.group(
      {
        siteName: ['', [Validators.required]],
        siteDescription: ['', [Validators.required]],
        siteCreatedOn: ['', [Validators.required]],
        siteActive: ['', [Validators.required]]
      }
    );
  }

  createNewSite() {
    console.log(this.siteForm.value);
  }

}

I removed 'ReactiveFormsModule, FormsModules' from all modules and the put it back in all module.ts file but it didn't solve the issue

Any idea? Thx!

CodePudding user response:

First of all, is view.html same as create.component.html? if yes, change <form formGroup="siteForm" (ngSubmit)="createNewSite()"> to <form [formGroup]="siteForm" (ngSubmit)="createNewSite()"> and you have to import ReactiveFormsModule, FormsModules

CodePudding user response:

Ok i found my error. siteModule was not declared in the entities/entities.module.ts which is the parent folder of site [entities/site/site.module.ts]!

Thx for you answers anyway:)

  • Related