【发布时间】:2016-03-22 05:40:17
【问题描述】:
使用 Angular 2 并尝试让这个简单的代码工作。 但我一直收到以下错误:
异常:无法解析 Tab(未定义)的所有参数。确保 它们都有有效的类型或注释。
到目前为止,ng2 没有将 constructor(tabs:Tabs) {… 注入到构造函数中
这是完整的代码:
///<reference path="../../typings/zone.js/zone.js.d.ts"/>
import {Component, Input} from 'angular2/core';
@Component({
selector: 'tab',
template: `
<ul>
<li *ngFor="#tab of tabs" (click)="selectTab(tab)">
{{tab.tabTitle}}
</li>
</ul>
<ng-content></ng-content>
`,
})
export class Tab {
@Input() tabTitle: string;
public active:boolean;
constructor(tabs:Tabs) {
this.active = false;
tabs.addTab(this);
}
}
@Component({
selector: 'tabs',
directives: [Tab],
template: `
<tab tabTitle="Tab 1">
Here's some content.
</tab>
`,
})
export class Tabs {
tabs: Tab[] = [];
selectTab(tab: Tab) {
this.tabs.forEach((myTab) => {
myTab.active = false;
});
tab.active = true;
}
addTab(tab: Tab) {
if (this.tabs.length === 0) {
tab.active = true;
}
this.tabs.push(tab);
}
}
发送
肖恩
【问题讨论】:
标签: angular