Angular 2(点击)不起作用

时间:2017-08-07 15:34:48

标签: angular

我无法理解这里的错误在哪里。

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>

当我点击按钮时,没有任何反应。

1 个答案:

答案 0 :(得分:1)

我已在此plunker中对其进行了测试,但它确实按预期工作。显然,您的设置有问题。控制台说什么? (在浏览器中,按F12并检查错误。)

唯一的区别是我使用了内嵌模板:

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>`
})
相关问题