3.3 (event) 事件绑定 - RLwu/angular-start GitHub Wiki

(event) - 事件绑定 在模板中为元素添加事件监听很简单,使用一对小括号包裹事件名称,并绑定 到表达式即可:

DOM对象click事件添加监听函数onClick()。

另一种等效的书写方法是在事件名称前加on-前缀:

@View({template : `<h1 on-click="onClick()">HELLO</h1>`})

例子

import {Component,View} from "angular2/core";
import {bootstrap} from "angular2/platform/browser";

@Component({selector:"ez-app"})
@View({
	template:`	
		<h1>Your turn! <b>{{sb}}</b></h1>
		<button (click)="roulette()">ROULETTE</button>
	`
})
class EzApp{
	constructor(){
		this.names = ["Jason","Mary","Linda","Lincoln","Albert","Jimmy"];
		this.roulette();
	}
	//轮盘赌
	roulette(){
		var idx = parseInt(Math.random()*this.names.length);
		this.sb = this.names[idx];
	}
}
		
bootstrap(EzApp);

⚠️ **GitHub.com Fallback** ⚠️