naveen
naveen

Reputation: 937

how to make getter and setter in angular of input field model property?

could you please tell me how to make getter and setter of input field property . I tried like this

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
      private _keyValue: string;
      get KeyValue(): string{
        return this._keyValue;
    }

    set KeyValue(value: string){
      console.log('====')
        this._keyValue = value;
    }

  name = 'Angular 6';
}

when I typed on input field my console is not printed why? here is my code https://stackblitz.com/edit/angular-vnvv6b?file=src%2Fapp%2Fapp.component.ts

Upvotes: 1

Views: 381

Answers (1)

user184994
user184994

Reputation: 18281

You're not using the setter you created, you're directly binding to _keyValue

To bind to your setter, you must use

<input [(ngModel)]="KeyValue"/>

As KeyValue is the name of your setter

Upvotes: 1

Related Questions