Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active January 5, 2018 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinchisholm/4e80e69f59b61dd2f27545f52160d536 to your computer and use it in GitHub Desktop.
Save kevinchisholm/4e80e69f59b61dd2f27545f52160d536 to your computer and use it in GitHub Desktop.
How do I set up an Angular click-event handler?

JavaScript Logo

Code Examples for my Blog Post: How do I set up an Angular click-event handler?

Video Demonstration

Creating an Event Handler with the Angular Click attribute - Kevin Chisholm Video

Setup

  • Clone this repo:
git clone https://github.com/kevinchisholm/video-code-examples
  • Move into the project directory:
cd angular/templates-and-data/click-attribute/
  • Install Dependencies:
npm install

Local Web Server Instructions

Start the local web server:

npm start

Open the example web page:

http://localhost:3000/
import {Component} from '@angular/core';
@Component({
selector: 'home',
templateUrl: 'src/home/home.html'
})
export class HomeComponent {
content: string = "";
count: number = 0;
setMessage1 () {
this.content = 'This is message # 1';
}
}
<button (click)="setMessage1()">
Set Message # 1
</button>
<button (click)="content = 'This is message # 2'">
Set Message # 2
</button>
<button (click)="count = count + 1">
I've been clicked {{count}} times
</button>
<div>
<h2>Content</h2>
<p>{{content}}</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment