我在加载一个类到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);

服务/ NameService.ts

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

我一直得到一个错误消息说没有NameService的提供者。

有人能帮我找出我的代码的问题吗?


当前回答

从Angular 2 Beta开始:

将@Injectable添加到你的服务中:

@Injectable()
export class NameService {
    names: Array<string>;

    constructor() {
        this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"];
    }

    getNames() {
        return this.names;
    }
}

在你的组件配置中添加提供商:

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

其他回答

在app.module.ts文件中将服务添加到providers[]数组中。 像下面的

//这里我的服务是CarService

app.module.ts

import {CarsService} from './cars.service';

providers: [CarsService] // you can include as many services you have 

Angular 2已经改变了,你的代码顶部应该是这样的:

import {
  ComponentAnnotation as Component,
  ViewAnnotation as View, bootstrap
} from 'angular2/angular2';
import {NameService} from "./services/NameService";

@Component({
  selector: 'app',
  appInjector: [NameService]
})

此外,你可能想在你的服务中使用getter和setter:

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

然后在你的应用程序中,你可以简单地做:

this.names = nameService.names;

我建议你去plnkr。co,然后创建一个新的Angular 2 (ES6)插件,让它先在里面工作。它会为你安排好一切。一旦它在那里工作,将其复制到您的其他环境中,并对该环境中的任何问题进行分类。

您需要将其添加到providers数组中,该数组包含组件上的所有依赖项。

请看angular文档中的这一部分:

在组件中注册提供者 下面是修改后的HeroesComponent,它注册了HeroService 提供者数组。

import { Component } from '@angular/core';

import { HeroService } from './hero.service';

@Component({
  selector: 'my-heroes',
  providers: [HeroService],
  template: `
  <h2>Heroes</h2>
  <hero-list></hero-list>
  `
})
export class HeroesComponent { }

When to use NgModule versus an application component On the one hand, a provider in an NgModule is registered in the root injector. That means that every provider registered within an NgModule will be accessible in the entire application. On the other hand, a provider registered in an application component is available only on that component and all its children. Here, the APP_CONFIG service needs to be available all across the application, so it's registered in the AppModule @NgModule providers array. But since the HeroService is only used within the Heroes feature area and nowhere else, it makes sense to register it in the HeroesComponent. Also see "Should I add app-wide providers to the root AppModule or the root AppComponent?" in the NgModule FAQ.

所以在你的情况下,简单地改变注入到提供商如下所示:

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

在Angular的新版本中,@View和其他一些东西也消失了。

欲了解更多信息,请访问这里。

你应该在你的AppModule的NgModule元数据的providers数组中注入NameService。

@NgModule({
   imports: [BrowserModule, ...],
   declarations: [...],
   bootstrap: [AppComponent],
   //inject providers below if you want single instance to be share amongst app
   providers: [MyService]
})
export class AppModule {

}

如果你想在不考虑应用程序状态的情况下为特定组件级别创建依赖项,那么你可以将依赖项注入组件提供者元数据选项,如所示的accepted @Klass answer。

将@Injectable添加到你的服务中:

export class NameService {
    names: Array<string>;

    constructor() {
        this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"];
    }

    getNames() {
        return this.names;
    }
}

在你的组件中添加提供商:

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

如果你想在整个应用程序中访问你的服务你可以传入应用程序提供程序