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 Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

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