1

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

I have already tried all the solutions already add ReactiveFormsModule , FormsModule also tried [formGroup] and [FormGroup] still getting the same error

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

 imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
  ]

my form

<form [formGroup]="addPostForm" (ngSubmit)="addNewPost()">
     <ion-item>
          <ion-label class="grey" position="stacked">Caption </ion-label>
          <ion-input type="text" formControlName="caption"></ion-input>
     </ion-item>
     <ion-button expand="block" type="submit" shape="round">Save</ion-button>
</form>

add-post.page.ts

import { Component, OnInit } from '@angular/core';
import { IonSlides } from '@ionic/angular';
import { ApiService } from '../services/api.service';
import { Router } from "@angular/router"
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule, FormsModule } from '@angular/forms';


@Component({
  selector: 'app-add-post',
  templateUrl: './add-post.page.html',
  styleUrls: ['./add-post.page.scss'],
})
export class AddPostPage implements OnInit {

    addPostForm: FormGroup;
    picture = null;
    submitted = false;
    success = false;

    constructor(
        private api: ApiService,
        private FormBuilder: FormBuilder,
        private router: Router,
    ) {
        this.addPostForm = this.FormBuilder.group({
            caption: ['']
        });
    }

Error

Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
Pratik Solanki
  • 75
  • 1
  • 2
  • 10

2 Answers2

0

In your component.ts file (the component.ts file for which the form code above has it in the HTML page), please initiate the form

Something like this:

addPostForm: FormGroup = new FormGroup({
  caption: new FormControl('', [Validators.required]) // If validators are required, otherwise you can remove the [Validators.required] part.
});


Srikar Phani Kumar M
  • 1,069
  • 1
  • 3
  • 8
0

You need to setup form in app.component.ts file. Please refer below code for the same,

import { Component } from '@angular/core';
import { AuthenticationService } from './services/authentication.service';
import { Router } from '@angular/router';

import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent {
  navigate : any;
  //Need to define addPostForm here.
  addPostForm = new FormGroup({
    caption: new FormControl('')
  });
  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private authenticationService: AuthenticationService,
    private router: Router
  ) {
    this.sideMenu();
    this.initializeApp();
  }

For more reference please refer this angular docs for reactive forms.

Note: If your component is imported in another module then in other module you need to import ReactiveFormsModule in this module.

Aman Gojariya
  • 1,289
  • 1
  • 9
  • 21