Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting components Angularjs2

I'm just testing out Angularjs2.0 but I'm running into issue injecting a service to a class. Here is my code:

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

@Component({
    selector: 'meeting-form',
    appInjector: [MeetingService]
})

@View({
    template: `
        <ul>
            <li *ng-for="#meeting of meetings">
                Meeting Room: meeting room {{meeting.room}}
                Date: {{meeting.date}}
                Time: {{meeting.time}}
            </li>
        </ul>
            <select #room>
                <option value="1">Meeting Room 1</option>
                 <option value="2">Meeting Room 2</option>
            </select>
            <input type="date" #date>
            <input type="time" #time>
            <button (click)="reserveMeeting(room.value, date.value, time.value)">Reserve</button>

    `,
    directives: [NgFor]
})

class MeetingFormComponent{
    meetings: Array;

    constructor(meetingService: MeetingService){
        this.meetings = meetingService.getMeetings();
    }

    reserveMeeting(room: number, date: string, time: string, meetingService: MeetingService){
        meetingService.postMeeting(room, date, time);
    }
}

class MeetingService {
    http: HttpService;

    constructor(http: HttpService) {
        this.http = http;
    }
    getMeetings() {
        return this.http.get('/meetings').then(function(data){
            return data.meetings;
        })
    }

    postMeeting(room: number, date: string, time: string){
        return this.http.post('/meetings', {"room": room, "date": date, "time": time}).then(function(data){
           return data.meeting;
        })
    }
}

bootstrap(MeetingFormComponent);

I get this error:

EXCEPTION: Error during instantiation of Token(Promise<ComponentRef>)!. ORIGINAL EXCEPTION: Cannot resolve all parameters for MeetingFormComponent(undefined). Make sure they all have valid type or annotations.

I'm looking at the docs and it seems like that is exactly how they are injecting their service. Am I missing a step? I've also tried bootstraping the service class but that didn't work.

like image 585
ThreeAccents Avatar asked Sep 22 '15 16:09

ThreeAccents


People also ask

What is AngularJS dependency injection?

What is AngularJS Dependency Injection? AngularJS Dependency injection defines, how the various components of the software are dependent on each other. Instead of hard-coding a component within another component, a component is given their own dependencies using dependency injection.

How do you inject a component in angular?

In Angular.JS, dependencies are injected by using an “injectable factory method” or “constructor function”. These components can be injected with “service” and “value” components as dependencies. We have seen this in an earlier topic with the $http service.

What is the AngularJS injector subsystem?

The AngularJS injector subsystem is in charge of creating components, resolving their dependencies, and providing them to other components as requested. Using Dependency Injection

What is di in AngularJS?

Dependency Injection (DI) is a software design pattern that deals with how components get hold of their dependencies. The AngularJS injector subsystem is in charge of creating components, resolving their dependencies, and providing them to other components as requested.


1 Answers

Mark your service as injectable like this:

import {Injectable} from 'angular2/core';

@Injectable()
class MeetingService { ... }

You can read more about that HERE

Update if your code is all in 1 file as you have it shown above, then you will need to move the definition of your service above the component, because classes in ES6 (ES2015) are not hoisted as functions are in ES5.

like image 136
Brocco Avatar answered Nov 15 '22 08:11

Brocco