我试图在点击事件上更新按钮的标签。我有以下代码。该方法setText()
不会在点击事件上被调用。
此外,如果尝试将调用包含在模板本身中
<button (click)="setText('new name')"></button>
有用。
但我想公开这个api并想调用类似的方法
<mybutton (click)="setText('new name')"></button>
有人可以建议这里的错误是什么吗?我正在使用 angular2 beta。
应用程序.ts
import {Component, View, Input, Output, EventEmitter} from 'angular2/core';
@Component({
selector: 'mybutton',
})
@View({
template: `<button>{{label}}</button>`
})
export class MyButton {
@Input() label: string;
@Output() click = new EventEmitter();
setText(newName: string) {
this.label = newName;
}
}
索引.html
<html>
<head>
<title>Angular 2 QuickStart</title>
<!-- 1. Load libraries -->
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages : {
app : {
format : 'register',
defaultExtension : 'js'
}
}
});
System.import('app/boot').then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<mybutton [label]="'My Button'" (click)="setText('New Name')" ></mybutton>
</body>
</html>