2017-08-07 145 views
0

我无法理解这里的错误在哪里。Angular 2(click)不起作用

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> 

当我按一下按钮,没有任何反应。

+0

创建可能的话 – Faisal

+0

,能得到任何控制台误差的plunker?据我所知,这应该起作用。 –

+0

按预期工作,这里是一个plunk:https://plnkr.co/edit/0eGfLSzZjhr9a6vtDhdC – Faisal

回答

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>` 
}) 
+0

谢谢。问题在于导入模块的顺序错误 –