ItSolutionStuff.com

Angular 18 Button Click Event Example

By Hardik Savani β€’ September 28, 2024
Angular

This article will provide some of the most important example angular 18 click event. This article goes in detailed on angular 18 button click event example. This post will give you a simple example of button click event in angular 18. This article will give you a simple example of click event in angular 18 example.

Angular 18'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 18

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

Let's follow the steps:

Step 1: Create Angular 18 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 18 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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Angular 18 Add Social Media Share Buttons Example

Read Now β†’
β˜…

Angular 18 Create Custom Directive Example

Read Now β†’
β˜…

How to Create Interface in Angular 18?

Read Now β†’
β˜…

Angular 18 Async Pipe Example Tutorial

Read Now β†’
β˜…

How to Generate QR Code in Angular 18?

Read Now β†’
β˜…

Angular 18 Create Custom Pipe Example Tutorial

Read Now β†’
β˜…

Angular 18 Routing and Navigation Example Tutorial

Read Now β†’
β˜…

Angular 18 @for Loop with Index Example

Read Now β†’
β˜…

Angular 18 @for and @empty For Loop Example

Read Now β†’
β˜…

Angular 18 @switch, @case and @default Example

Read Now β†’
β˜…

Angular 18 Conditional Statements @if, @else if, and @else Example

Read Now β†’
β˜…

Angular 18 CRUD Application Tutorial Example

Read Now β†’