In Angular, you can bind events to a element.
In Angular, you can bind events to a element.
The syntax to bind elements is as follows:
(eventToBind)=""
For example:
<h1 (click)="goToHomePage()">Home</h1>
In the code snippet, we bound the click event to the header element. When the user clicks on the header element, it will call the goToHomePage() function.
Here are some events that you can bind to:
Filtering event keys
Of all the events, the keyup, keydown, and keypress, events can be filtered. This mean that you can specify which key you want to trigger a function. For example, if you want to fire an function when you press a key.
<input (keyup.enter)="goToHomePage()" />
In the code snippet, a keyup event is bound to an input element. When the user press the enter key while the input element is focused, it will call the goToHomePage() function.
Demonstration
To demonstrate, follow along with this tutorial. We will add a input element in our app.component.html file and bind they keyup.enter event to it.
<input (keyup.enter)="goToHomePage()" />
In the app.component.ts file, create the goToHomePage() function. This function will just be an alert() message.
Now, when you start the application using the ng serve command, you should see a button when you go to localhost:4200. When you click on the button, it will pop out an alert, displaying - GOING TO HOME PAGE.
=============
One of the major advantages of using Angular is the ability to easily handle user interactions with the DOM.
<div>Hello World</div>
In order for us to give an element in the DOM a click feature, we have to define the element in the HTML page and give it an id so we can reference it later.
<div id="clickMeExample">Hello World</div>
Once we have done that, we have to write some JavaScript code to get the element and then we can attach an event handler to handle our events.
let clickMeExample = document.getElementById("clickMeExample");
Let’s take a look at the same example when we apply Angular.
<div (click)="clickFunction()">Hello World</div>
clickFunction(){
<div (click)="clickMeExample()">Hello World</div>
Once we have done that, the last thing we need to do is to define that function in the TypeScript file. Go to the app component TypeScript file and define the function.
clickMeExample(){
Save the project and start the local server. Open the browser and go to localhost:4200. Then click on the element.
<input (keyup)="keyupExample()" />
Tthen go to the app component Typescript to define that function.
keyupExample(){
Save the project and go to the browser. Click on the input element and press a key.
You may wonder, how we can get the value of the key that was pressed on. Whenever an event gets triggered, the event will return an $event object with information about the event. We can use that event object that is returned to us and pass it into the function that it is calling and get the value of the key that was pressed.
<input (keyup)="keyupExample($event)" />
However, we need to make the function accept that object. Go back to the app component TypeScript file and locate the function definition, add a parameter to accept that $event object as an argument.
keyupExample(event){
The event object contains a property call key which returns the value of the key that was pressed. Store it in a variable and pass it into the alert function.
keyupExample(event){
Now save the project and go to the browser. When we press and let go of a key, it will print the key that was pressed.
Recap