One issue I'm facing is that I have multiple components in my Angular 2 application that require the same dependency. This specific dependency needs a string for the constructor. How can I instruct angular2 to use a specific instance of this type for Dependency Injection?
For example:
In ChatUsers.ts:
@Component({
selector: "chat-users"
})
@View({
directives: [],
templateUrl: '/js/components/ChatUsers.html'
})
export class ChatUsers {
constructor(public currentUser : User) {
}
}
And in app.ts:
/// <reference path="../libs/typings/tsd.d.ts" />
import {Component, View, bootstrap} from 'angular2/angular2';
import {User} from "User";
// How do I instantiate a user (e.g. new User('John')) and use it for DI?
@Component({
selector: 'chat-app'
})
@View({
directives: [ ],
template: `
<div> Some text
</div>`
})
class ChatApp {
constructor(public user: User) {
// Perform actions using the user object
}
}
bootstrap(ChatApp, [ User ]);
In User.ts:
export class User {
name: string;
constructor(name: string) {
this.name = name;
}
}
When running this code, an error occurs:
Cannot resolve all parameters for User(?). Make sure they all have valid type or annotations.
I am currently using the latest version of Angular 2: 2.0.0-alpha.44