Angular 17 Button Click Event Example

By Hardik Savani May 2, 2024 Category : Angular

Hey Folks,

This is a short guide to angular 17 click events. You can see the angular 17 button click event example. I just explained step by step button click event in angular 17. This article will give a simple example of click event in angular 17 example.

Angular 17's button click event handling remains consistent with previous versions. Developers can use the (click) event binding to trigger functions or actions when a button is clicked. This event binding is a fundamental part of Angular's event-driven architecture, allowing for interactive user experiences. With Angular's continued evolution, enhancements to performance, features, and tooling may be expected, but the core concepts like button click event handling remain stable and familiar to developers.

In this example, we will create a function clickMe() and bind it to the button click event. We will take the variable count and add that variable based on the button click. So, let’s take a look at the simple steps:

So, let's follow the following steps:

Step for Button Click Event in Angular 17

  • Step 1: Create Angular 17 Project
  • Step 2: Update Ts File
  • Step 3: Update HTML File
  • Run Angular App

Let's follow the steps:

Step 1: Create Angular 17 Project

You can easily create your angular app using below command:

ng new my-new-app

Step 2: Update Ts File

here, we need to update ts file as like bellow with lat and long variable:

src/app/app.component.ts

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

import { CommonModule } from '@angular/common';

@Component({

selector: 'app-root',

standalone: true,

imports: [CommonModule],

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent {

count: number = 0;

clickMe() {

this.count++;

}

}

Step 3: Update HTML File

here, we need to update html file as like bellow code:

src/app/app.component.html

<div class="container">

<h1>Angular 17 Button Click Event Example - ItSolutionStuff.com</h1>

<button type="button" class="btn btn-success" (click)="clickMe()">Click me!</button>

<strong>Counter: {{ count }}</strong>

</div>

Run Angular App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Angular app:

ng serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:4200

Preview:

now you can check it.

I hope it can help you...

Shares