Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular no provider for NameService

I've got a problem loading a class into an Angular component. I've been trying to solve it for a long time; I've even tried joining it all in a 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); 

services/NameService.ts

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

I keep getting an error message saying No provider for NameService.

Can someone help me spot the issue with my code?

like image 347
M.Svrcek Avatar asked Jun 01 '15 17:06

M.Svrcek


People also ask

Is there a way to add providers in Angular 2?

@nathanhleung it seems like providers made it's way into the release also. In Angular 2 there are three places you can "provide" services: "The bootstrap provider option is intended for configuring and overriding Angular's own preregistered services, such as its routing support."

How to include a NameService in a singleton component?

If you only want one instance of NameService across your entire app (i.e., Singleton), then include it in the providers array of your root component: @Component ( { providers: [NameService], ... )} export class AppComponent { ... }

Why is the app_config Service registered in the appmodule @ngmodule?

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.

What is @injectable service in angular?

The service itself is a class that the CLI generated and that's decorated with @Injectable (). By default, this decorator has a providedIn property, which creates a provider for the service. In this case, providedIn: 'root' specifies that Angular should provide the service in the root injector.


2 Answers

You have to use providers instead of injectables

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

Complete code sample here.

like image 74
Klas Mellbourn Avatar answered Oct 17 '22 05:10

Klas Mellbourn


In Angular 2 there are three places you can "provide" services:

  1. bootstrap
  2. root component
  3. other components or directives

"The bootstrap provider option is intended for configuring and overriding Angular's own preregistered services, such as its routing support." -- reference

If you only want one instance of NameService across your entire app (i.e., Singleton), then include it in the providers array of your root component:

@Component({    providers: [NameService],    ... )} export class AppComponent { ... } 

Plunker

If you would rather have one instance per component, use the providers array in the component's configuration object instead:

@Component({    providers: [NameService],    ... )} export class SomeOtherComponentOrDirective { ... } 

See the Hierarchical Injectors doc for more info.

like image 21
Mark Rajcok Avatar answered Oct 17 '22 06:10

Mark Rajcok