我有问题加载类到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; } }
你必须使用提供程序,而不是注射
原文链接:https://www.f2er.com/angularjs/147041.html@Component({ selector: 'my-app',providers: [NameService] })