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.
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.
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.
The AngularJS injector subsystem is in charge of creating components, resolving their dependencies, and providing them to other components as requested. Using Dependency Injection
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With