Pravu
Pravu

Reputation: 608

Auto Call Function When Input Field Has Value

I try to implement auto call function when input field has value. When input field has value it can auto call function. But I don't know how to make it work. I provide simple code and DEMO as your reference.

HTML

<div class="pb-1">
  <input maxlength="10" placeholder="Text" (ngModelChange)="didModify(event)" (input)="didModify()" [(ngModel)]="text1">
</div>
<p>{{changeCounter}}</p>

Component

  text1 = 'test count';
  changeCounter = 0;

  didModify() {
    this.changeCounter = this.changeCounter + 1;
    return this.changeCounter;
  }

Upvotes: 3

Views: 1719

Answers (1)

Nikhil G
Nikhil G

Reputation: 2466

You will have to implent OnInit and then you can do it. Please check below code.

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
  name = 'Angular 6';
  text1 = 'test count';
  changeCounter = 1;
  ngOnInit() { this.didModify(); }

  didModify() {
    this.changeCounter = this.changeCounter + 1;
    return this.changeCounter;
  }
}

Upvotes: 4

Related Questions