2

I am not sure if it's a bug or I am doing something wrong (prob the latter). I am trying to manually instantiate a component named TodoService.

everything is great on the first 2 steps

var myProvider = new Provider(TodosService, {
  useFactory: (todoAction, http, todoStatsModel, appStore) => {
  return new todoAction(TodoAction, http, todoStatsModel, appStore)
},
  deps: [TodoAction, Http, TodoStatsModel, AppStore]
});
var injector =  Injector.resolveAndCreate([myProvider]);

but blows up when I try to get it so I can use it

var myTodoService = injector.get(TodosService)

am I doing something wrong (possible on the get)?

enter image description here

1
  • I wonder why you use a custom injector. This shouldn't be necessary. Sure there are cases but it's better to avoid it if it's not necessary. Commented Feb 14, 2016 at 15:42

2 Answers 2

2
var injector =  Injector.resolveAndCreate([myProvider, TodoAction, Http, TodoStatsModel, AppStore]);

I think you'd be better off not use use a factory with just

var injector =  Injector.resolveAndCreate(
    [TodoService, TodoAction, Http, TodoStatsModel, 
     provide(AppStore, {useValue: new AppStore('')}
     // or
     // provide(AppStore, {useFactory: () => {
     //     return new AppStore('');
     // })
    ]);
var myTodoService = injector.get(TodosService)
Sign up to request clarification or add additional context in comments.

6 Comments

I see now that with your statement I actaully get a more descriptive error, says missing ORIGINAL EXCEPTION: Cannot resolve all parameters for 'AppStore'(?). Make sure that... so I guess I need to resolve more dependencies
Exactly. Just add to providers what the AppStore constructor parameter type is.
I guess this leads to a new issue, my AppStore signature is: constructor(store: any); because it's not an Angular component, just a simple class, so it doesn't really have "dependencies", just need to be passed in a value... mmm.... possible?
This is a perfect case for creating a new Provider() (or using provide()) while it doesn't look like TodosService needs a provider.
going down the rabbit hole... it now compians it needs ConnectionBackend so I added, and now it complains that it needs RequestOptions so I added... and now it complains that: ORIGINAL EXCEPTION: Cannot resolve all parameters for 'RequestOptions'(?). Make sure tha.... which has a constructor signtaure of: 'constructor({method, headers, body, url, search}?: RequestOptionsArgs);' :( never ending...
|
0

Tx to Günter Zöchbaue who solved this for me... he is the man!!!

here is the code for everyone else:

constructor(private appStore:AppStore) {
    super();
}

private factoryService(){
    if (this.service)
        return;
    var injector = Injector.resolveAndCreate(
        [
            TodosService,
            TodoAction,
            HTTP_PROVIDERS,
            TodoStatsModel,
            provide(AppStore, {useValue: this.appStore})
        ]);

    this.service = injector.get(TodosService);
    service.sayHello('Sean');
}

basically manually creating "TodoService" Angular2 component through multiple dependencies (and even one vanilla non component (class) AppStore) and all via the injector... wooof...!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.