【发布时间】:2015-08-15 07:13:51
【问题描述】:
我在将类加载到 Angular 组件时遇到问题。我一直在尝试解决它很长时间;我什至尝试将它们全部加入一个文件中。我有的是:
Application.ts
/// <reference path="../typings/angular2/angular2.d.ts" />
import {Component,View,bootstrap,NgFor} from "angular2/angular2";
import {NameService} from "./services/NameService";
@Component({
selector:'my-app',
injectables: [NameService]
})
@View({
template:'<h1>Hi {{name}}</h1>' +
'<p>Friends</p>' +
'<ul>' +
' <li *ng-for="#name of names">{{name}}</li>' +
'</ul>',
directives:[NgFor]
})
class MyAppComponent
{
name:string;
names:Array<string>;
constructor(nameService:NameService)
{
this.name = 'Michal';
this.names = nameService.getNames();
}
}
bootstrap(MyAppComponent);
services/NameService.ts
export class NameService {
names: Array<string>;
constructor() {
this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"];
}
getNames()
{
return this.names;
}
}
我不断收到一条错误消息,上面写着No provider for NameService。
有人可以帮我找出我的代码的问题吗?
【问题讨论】:
-
从 Alpha 42 开始:@Component({ providers: [NameService] })
标签: typescript angular systemjs