D. Malakhov
D. Malakhov

Reputation: 165

Angular 2 (click) doesn't work

I can't understand where is a bug here.

add.component.ts:

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

@Component({
    selector: 'counter',
    templateUrl: './add.component.html'
})
export class CounterComponent {
    public currentCount = 0;

    public incrementCounter() {
        this.currentCount++;
    }
}

add.component.html:

<h1>Counter</h1>

<p>This is a simple example of an Angular component.</p>

<p>Current count: <strong>{{ currentCount }}</strong></p>

<button (click)="incrementCounter()">Increment</button>

When I click on the button, nothing happens.

Upvotes: 1

Views: 2002

Answers (1)

Kim Kern
Kim Kern

Reputation: 60347

I have tested it in this plunker and it does work as expected. Apparently, something is wrong with your setup. What does the console say? (In your browser, press F12 and check for errors.)

Only difference is I used an inline template:

template: `<h1>Counter</h1>
<p>This is a simple example of an Angular component.</p>
<p>Current count: <strong>{{ currentCount }}</strong></p>
<button (click)="incrementCounter()">Increment</button>`
})

Upvotes: 1

Related Questions