1

I'm following google's angular2 quick start (no Github for these examples?!)

Everything was working smoothly until it was time to inject a service.

This is my code:

import {Component, View, bootstrap, NgFor} from 'angular2/angular2';

class FriendsService {
    names: Array<string>;

    constructor(){
        this.names = ['Aviad', 'Chen', 'Yarden'];
    }
}

@Component({
    selector: 'display',
    appInjector: [FriendsService]
})
@View({
    template:
    <p>My name: {{ myName }}</p>
    <p>Friends:<p>
    <ul>
        <li *ng-for="#name of names">
            {{name}}
        </li>
    </ul>
    ,
    directives: [NgFor]
})

export default class DisplayComponent {
    myName: string;
    names: Array<string>;

    constructor(friendsService: FriendsService){
        this.myName = 'Alice';
        this.names = friendsService.names;
    }
}

I ran into a lot of exceptions but the first one and what I think is the most relevant is:

EXCEPTION: No provider for FriendsService! (DisplayComponent -> FriendsService) angular2.dev.js:22367 STACKTRACE: angular2.dev.js:22367 Error: DI Exception

    at NoBindingError.BaseException (angular2.dev.js:7735)
    at NoBindingError.AbstractBindingError (angular2.dev.js:9029)
    at new NoBindingError (angular2.dev.js:9052)
    at Injector.execute._proto._throwOrNull (angular2.dev.js:27552)
    at Injector.execute._proto._getByKeyDefault (angular2.dev.js:27597)
    at Injector.execute._proto._getByKey (angular2.dev.js:27545)
    at Injector.execute._proto._getByDependency (angular2.dev.js:27533)
    at Injector.execute._proto._instantiate (angular2.dev.js:27430)
    at Injector.execute._proto._new (angular2.dev.js:27403)
    at InjectorInlineStrategy.execute.protoStrategy.instantiateBinding (angular2.dev.js:27192) angular2.dev.js:22367 ERROR CONTEXT:

One thing that may be relevant is that the <display> component is a directive of the <my-app> component:

import {Component, View, bootstrap} from 'angular2/angular2';
import DisplayComponent from './show-properties';

@Component({
    selector: 'my-app'
})
@View({
    template: '<display>',
    directives: [DisplayComponent]
})
class AppComponent { }

bootstrap(AppComponent);

1 Answer 1

1

This did the trick:

@Component({
    selector: 'display',
    bindings: [FriendsService] // instead of appInjector
})

When anything else remained the same as above.

Anyway according to the changelog this was a breaking change (thanks jesse Good):

BREAKING CHANGES

THe appInjector property has been removed. Instead use viewInjector or hostInjector.

I guess that the Angular2 Quick Start guide is not as up to date as I thought.

Sign up to request clarification or add additional context in comments.

2 Comments

Was just about to post an answer. I recommend reading the changelog. appInjector was removed in alpha-29 and the current is alpha-37. As you can see they are way behind in the documentation ;). (The developers are aware of this and are trying to improve right now)
I was expecting them to update it accordingly ;) But you are right, thanks for the tip - I will definitely use it now on.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.