我有问题加载类到Angular2组件。我试图解决很长时间,甚至试图将其添加到单个文件。我有的是:
应用程序
/// <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;
}
}
所有的时间,我得到错误消息说“没有提供者NameService”…有人可以帮助我发现我的代码的小问题吗?
你必须使用提供程序,而不是注射
@Component({
selector: 'my-app',providers: [NameService]
})
Complete code sample here。