【发布时间】:2016-01-08 10:17:35
【问题描述】:
我正在尝试在单击事件上更新按钮的标签。我有以下代码。方法setText() 没有在点击事件中被调用。
如果尝试在模板本身中包含调用
<button (click)="setText('new name')"></button>
它有效。
但是我想公开这个api并想调用类似的方法
<mybutton (click)="setText('new name')"></button>
有人可以建议这里的错误是什么吗?我正在使用 angular2 beta。
app.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;
}
}
index.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>
【问题讨论】:
标签: angular angular2-directives