typescript – Angular2没有NameService的提供程序

前端之家收集整理的这篇文章主要介绍了typescript – Angular2没有NameService的提供程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有问题加载类到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

原文链接:https://www.f2er.com/angularjs/147041.html

猜你在找的Angularjs相关文章