0

I am new to angular. I am learning about angular forms and though I copied the below code from a tutorial, I still get the error:

Property 'updateEmployeeName' does not exist on type 'ContactFormComponent'

Typescript

import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
  selector: 'app-contact-form',
  templateUrl: './contact-form.component.html',
  styleUrls: ['./contact-form.component.css']
})

export class ContactFormComponent  {
  employeeName= new FormControl('');

  updateEmployeeName(){
    this.employeeName.setValue('john');
  }

}

HTML

<label>
  Employee Name: 
  <input type="text" [formControl]="employeeName">
</label>
jps
  • 20,041
  • 15
  • 75
  • 79

2 Answers2

1

There doesn't seem to be anything wrong with the code. Can you try to stop your development server and run ng serve again? As mentioned here, sometimes Angular does not reflect the changes you make during development.

Stackblitz: https://stackblitz.com/edit/angular-ivy-pxyrfx?file=src/app/app.module.ts

rvrsev
  • 166
  • 1
  • 10
0

It Doesn't seem like you copy/pasted all the relevant code in your question.
The error you get means that you are using updateEmployeeName() somewhere in your code, but all I can see is a declaration of updateEmployeeName() inside your ts file.
updateEmployeeName() is not used in the html and it is not used in the ts files you posted with your question.
Can you please double check if you used updateEmployeeName somewhere else?

Elmehdi
  • 1,390
  • 2
  • 11
  • 25
  • sorry i didn't posted the rest of code:

    Value: {{EmployeeName.value}}

    i did called the updateEmployeeName function on button click
    – Waseem Abbas Sep 28 '20 at 07:43
  • this html in inside contact-form.component.html right ? – Elmehdi Sep 28 '20 at 08:51
  • must be something bad going on in your environement, it workd just fine for me: https://stackblitz.com/edit/angular-ivy-sjhj3g?file=src/app/contact-form/contact-form.component.html – Elmehdi Sep 28 '20 at 11:14
  • and don't forget to change EmployeeName.value to employeeName.value – Elmehdi Sep 28 '20 at 11:15