Angular2没有供应商名称服务 [英] Angular2 no provider for NameService

查看:132
本文介绍了Angular2没有供应商名称服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与加载类问题成Angular2组件。我试图解决长的时间,甚至试图将它添加到单个文件。我拥有的是:

I've got problem with loading class into Angular2 component. I'm trying to solve for long time, even tried to add it to single file. What i have is:

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);

服务/ NameService.ts

export class NameService {
    names: Array<string>;
    constructor() {
        this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"];
    }
    getNames()
    {
        return this.names;
    }
}

一直以来我得到错误信息没有供应商名称服务...有人可以帮助我发现小问题,我的code?

All the time I'm getting error message saying "No provider for NameService" ...can someone help me spot that small issue with my code?

推荐答案

Angular2阿尔法35 你必须使用绑定而不是注射剂

@Component({
    selector: 'my-app',
    bindings: [NameService]
})

完整code这里样本。

由于甲型42,使用提供商

@Component({
    selector: 'my-app',
    providers: [NameService]
})

这篇关于Angular2没有供应商名称服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆