I'm trying to use the Angular 2 DI system to automatically handle the dependencies of my services. I would like to use annotation for the service itself, rather than using the second bootstrap() parameter to specify all injection services.
What i got
Low level service:
services/role-store.ts
export class RoleStore { constructor() {
A high-level service, which depends on a low-level service:
services/user-store.ts
import {Injectable} from 'angular2/angular2'; import {RoleStore} from './role-store.ts'; @Injectable() export class UserStore { constructor( roleStore: RoleStore ) { this.roleStore = roleStore;
A component that depends on a high-level service:
components/user-dir.ts
import {Component, View, CORE_DIRECTIVES} from 'angular2/angular2'; import {UserStore} from '../services/user-store'; @Component({ selector: 'user-dir', bindings: [UserStore] }) @View({ template: '<!-- inline template -->', directives: [CORE_DIRECTIVES] }) export class UserDir { constructor( data: UserStore ) { this.userStore }
Root component for bootstrap:
app.ts
import {Component, View, bootstrap} from 'angular2/angular2'; import {RoleStore} from './services/role-store'; import {UserDir} from './components/user-dir'; @Component({ selector: 'app' }) @View({ template: '<user-dir></user-dir>', styleUrls: ['./app.css'], directives: [UserDir] }) class App {} bootstrap( App, [RoleStore] );
Problem
bootstrap( App, [RoleStore] ) works, but I would prefer an annotation in user-store.ts that tells Angular to insert RoleStore .
Something like @Provide( RoleStore ) class UserStore {} .
Any tips?
angular
Tyler eich
source share