3

I am new to angular and I am working with template driven form.When I am using NgForm Its throwing error :

src/app/pages/add-booking/servicing/servicing.component.ts(40,17): error TS2304: Cannot find name 'Ngform'.

Below is my is code : servicing.component.html

<form novalidate #a="ngForm" (ngSubmit)="onSubmit(a)"  >
  <div class="row">
    <div class="col-md-6 col-sm-6 col-lg-6">
            <div class="form-group">
              <input type="text" id="num" name="num" class="form-control" required [(ngModel)]="registrationNumber" #num="ngModel">
              <label class="form-control-placeholder" for="num">Registration Number</label>
            </div>
            <div class="form-control-feedback"*ngIf="num.errors && (num.dirty || num.touched)">
                <p *ngIf="num.errors.required">Registration Number is required</p>
            </div>
        </div>
        <div class="col-md-6 col-sm-6 col-lg-6">
          <button type="button"(click)='someFunction()'>click</button>
        </div>
  </div>

  <div class="line">
  <h4><span>Vehicle Details</span></h4>
</div>

<div class="row">
  <div class="col-md-6 col-lg-6 col-sm-6">
    <div class="form-group">
      <select name="brand" id="brand" class="form-control"  [(ngModel)]="data.vehicleBrand" #brand="ngModel" required>
          <option *ngFor = "let sa of vehicleBrand" [value] = "sa"> {{sa}}</option>
      </select>
    </div>
  </div>
  <div class="col-md-6 col-lg-6 col-sm-6">
    <div class="form-control-feedback"*ngIf="brand.errors && (brand.dirty || brand.touched)" style="display:inline-block">
        <p *ngIf="brand.errors.required">Brand is required</p>
    </div>
  </div>
</div>

<div class="row">
  <div class="col-md-6 col-lg-6 col-sm-6">
    <div class="form-group">
      <select name="model" id="model" class="form-control"  [(ngModel)]="data.VehicleModel" #model="ngModel" required>
          <option *ngFor = "let sa of vehicleModel" [value] = "sa"> {{sa}}</option>
      </select>
      <label class="form-control-placeholder" for="ServiceAdvisor">Vehicle Model</label>
    </div>
  </div>

  <div class="col-md-6 col-lg-6 col-sm-6">
    <div class="form-control-feedback"*ngIf="model.errors && (model.dirty || model.touched)">
        <p *ngIf="model.errors.required">Model is required</p>
    </div>
  </div>
</div>

<div class="row">
  <div class="col-md-6 col-lg-6 col-sm-6">
    <div class="form-group">
      <select name="variant" id="variant" class="form-control"  [(ngModel)]="data.Vehiclevariant" #variant="ngModel" required>
          <option *ngFor = "let sa of vehicleVariant" [value] = "sa"> {{sa}}</option>
      </select>
      <label class="form-control-placeholder" for="ServiceAdvisor">Vehicle Variant</label>
    </div>
   </div>
    <div class="col-md-6 col-lg-6 col-sm-6">
      <div class="form-control-feedback"*ngIf="variant.errors && (variant.dirty || variant.touched)">
          <p *ngIf="variant.errors.required">Variant is required</p>
      </div>
    </div>
</div>
  <button type="submit" [disabled]="a.invalid">S</button>
</form>

And servicing.component.ts ::

import { Component, OnInit } from '@angular/core';
import {FormsModule} from '@angular/forms';
import {Servicing} from '../../model/AddServicing';
import {NgForm} from '@angular/forms';
import {ViewChild } from '@angular/core';
import {ServicingService } from '../../services/addServicing.service';


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

  @ViewChild('f') form: any;
  registrationNumber:string;
  private ServicingType: string[];
  private Complaints: string[];
  private service_advisor: string[];
  private creName: string[];

  public vehicle = [];

  constructor(private _data:ServicingService) { }


  ngOnInit() {
    this.ServicingType =  ['Body Repair', 'Servicing', 'Both'];
    this.Complaints =  ['First', 'Second', 'Third'];
    this.service_advisor = ['Anurag','Rohit','Rahul','Add Service Advisor'];
    this.creName = ['Rohnit','Mohit','Nitin','Add CRE'];
    this._data.getUser().subscribe(data => this.vehicle = data);
  }

  someFunction(){
    console.log(this.registrationNumber);
  }

  onSubmit(form:Ngform) {
     if (this.form.valid) {
         console.log("Form Submitted!");
         console.log(this.form.value.num);
         this.form.reset();
         }
       }

  }

Please Help me..I do not know from where is error is coming

2
  • You should include form module at your app module , follow the documentation angular.io/guide/forms Commented Feb 5, 2018 at 7:40
  • You have a typo in the onSubmit method; it shouldbe NgForm, not Ngform Commented Feb 5, 2018 at 8:41

4 Answers 4

4

You should import NgForm from @angular/forms to do that: i.e

import { NgForm }   from '@angular/forms';
Sign up to request clarification or add additional context in comments.

Comments

1

in the same file where you are using your function (component.ts) just import

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

you will get get solution.

Comments

0

Spelling mistake use NgForm instead of Ngform and also replace this.form to form only.check out following code snippet

onSubmit(form:NgForm) {
     if (form.valid) {
         console.log("Form Submitted!");
         console.log(form.value.num);
         form.reset();
         }
       }

5 Comments

You have a typo: you wrote NgFrom instead of NgForm
I think this doesn't solve the problem, since using this.form is the same of using form, and the error described in the question is another
The error is Cannot find name 'Ngform'. and my solution looks correct
Correcting a typo-question in the comments would be better
@Jota.Toledo : will take care of this
0

There are chances of arising this error in angular when we import 'NgForm' from '@angular/forms' in anycomponentname.component.ts file.

To resolve this error you may have added below code line in app.module.ts file

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

@NgModule({
---------------
---------------
  imports: [     
        BrowserModule,
        FormsModule
  ]
---------------
---------------
}) 

I hope that it will help you. Namaste

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.