Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Angular Get Started
  • Install Angular
  • Create Angular App
  • Angular Component
  • HTML Template
  • Event Binding
  • Two-way Data Binding
  • Interpolation
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Event Binding in Angular 2 (v11)

Events are handled in Angular using the following special syntax.

(target event name) = "template statement"

Bind the target event name within parentheses on the left of an equal sign, and event handler method or statement on the right.

Example: Binding Button Click Event
<button (click)="onShow()">Show</button>

Above, (click) binds the button click event and onShow() statement calls the onShow() method of a component.

Example: Handle Button Click Event in Component
@Component({
  selector: 'event-demo,
  template: '<button (click)="onShow()">Show</button>'
})
export class EventBindingDemoComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

  onShow() {
    alert('Show button clicked!');
  }

}

Alternatively, use the on- prefix, known as the canonical form:

Example: on-event
<button on-click="onShow()" >Show</button>

By default, an event propagates up to the parent container event. In the following example, click event propagates to click of div and will call both the onShow() and onDivClick() methods.

Example: Event Bubbling
<div (click)="onDivClick()">
    <button (click)="onShow()">Show</button>
</div>

$event

Mostly, when an event is raised, you may need to pass some value to the event handler function. This value can be number, string, or an object that contains information about an event.

You can pass the number or string value to the event handler function, as shown below.

Example: Passing Event Data
<button (click)="onShow(20)">Show</button>

Angular includes $event that contains the information about an event. The type of $event depends on the target event, e.g., if the target event is a native DOM element event, then it is an object.

Example: $event
<button (click)="onShow($event)">Show</button>

A component should define the onShow(event) method where the type of the parameter can be KeyboardEvent, MouseEvent, etc. If you don't know the exact event type, they use “any” type, as shown below.

Example: event Parameter
onShow(event:any) {
    console.log(event);
  }

If event is a native DOM element event then $event.target get DOM element reference using which you can access element's property e.g. $event.target.innerHTML returns the value of innerHTML property of a DOM element.

Example: Event Handling
<button (click)="onShow($event)">Show</button>

//component method
onShow(event:any) {
       <pre><code className="language-html"><button (click)="onShow($event)">Show</button>

//component method
onShow(event:any) {
        alert(event.target.innerHTML); // returns Show
  }

You can use $event.target in the template statement. The following example binds a component property to $event.target.value of the input box on the input event without using ngModel.

Example: Bind Event without ngModel
<input type="text" (input)="userName=$event.target.value" />
{{userName}}

Learn more about event binding.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.