Angular 4

How to bind to user input events to component event handlers?

Most of the DOM events are triggered by user input and bind to these events provides a way to get inputs from a user.

The following example shows a click event binding – [on-click.component.ts]
import { ComponentOnInit } from '@angular/core';

@Component({
  selector: 'app-on-click',
  templateUrl: './on-click.component.html',
  styleUrls: ['./on-click.component.css']
})
export class OnClickComponent implements OnInit {

  welcomeMsg = '';
  constructor() { }

  ngOnInit() { }

  onClick() {
    this.welcomeMsg = 'Welcome you, Anil!';
  }
}

And on-click.component.html -
<div class="msg">
  <button (click)="onClick()">Click Me!</button>
  <p>
    {{welcomeMsg}}
  </p>
</div>
OR
<!-- Canonical form, the (on-) prefix alternative -->
<div class="msg">
  <button on-click="onClick($event)">Click Me!</button>
  <p>
    {{welcomeMsg}}
  </p>
</div>

When the user clicks the button, Angular calls the onClick method from OnClickComponent.


ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^