angular2-injection

Injecting different providers while loading components using ComponentResolver in Angular 2

梦想与她 提交于 2019-11-30 16:37:28
Can we inject different provider while loading components dynamically? my-component @Component({ moduleId: module.id, selector: "my-component", template: "<div>my-component</div>", providers: [MyComponentService] }) export class MyComponent{ constructor(private ds: MyComponentService) { super(); } } some where else, this._cr.resolveComponent(MyComponent).then(cmpFactory => { let instance: any = this.testComponentContainer.createComponent(cmpFactory).instance; }); so in above code, while resolving MyComponent , provider for this MyComponentService will also be resolved, can we resolve it

Angular 2 HTTP “Cannot resolve all parameters for 'AppService'”

谁说胖子不能爱 提交于 2019-11-28 09:38:59
I tried to import the http provider into a service, but I'm getting the following error: Cannot resolve all parameters for 'AppService'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AppService' is decorated with Injectable. Here are some code snippets: <script src="~/es6-shim/es6-shim.min.js"></script> <script src="~/systemjs/dist/system-polyfills.js"></script> <script src="~/angular2/bundles/angular2-polyfills.js"></script> <script src="~/systemjs/dist/system.src.js"></script> <script src="~/rxjs/bundles/Rx.js"></script> <script src="

How to call component method from service? (angular2)

天涯浪子 提交于 2019-11-28 06:00:33
I want to create service, which can interact with one component. All another components in my app, should be able to call this service, and this service should interact with this component. How to call component method from service? @Component({ selector:'component' }) export class Component{ function2(){ // How call it? } } From this servive? @Injectable() export class Service { callComponentsMethod() { //From this place?; } } Interaction between components can be indeed achieved using services. You will need to inject the service use for inter-component communication into all the components

Getting instance of service without constructor injection

筅森魡賤 提交于 2019-11-27 11:38:39
I have a @Injectable service defined in bootstrap. I want to get the instance of the service without using constructor injection. I tried using ReflectiveInjector.resolveAndCreate but that seem to create a new instance. The reason I'm trying to do is I have a base component derived by many components. Now I need to access a service but I don't want to add it to the ctor because I don't want to inject the service on all of the derivative components. TLDR: I need a ServiceLocator.GetInstance<T>() UPDATE: Updated code for RC5+: Storing injector instance for use in components Yes,

Testing - Can't resolve all parameters for (ClassName)

扶醉桌前 提交于 2019-11-27 09:03:43
Context I created an ApiService class to be able to handle our custom API queries, while using our own serializer + other features. ApiService 's constructor signature is: constructor(metaManager: MetaManager, connector: ApiConnectorService, eventDispatcher: EventDispatcher); MetaManager is an injectable service that handles api's metadatas. ApiConnectorService is a service which is wrapping Http to add our custom headers and signature system. EventDispatcher is basically Symfony's event dispatcher system, in typescript. Problem When I test the ApiService , I do an initialization in beforeEach

angular 2 - Injected service in http error handler

冷暖自知 提交于 2019-11-27 06:14:49
问题 I have a method handleError() like in the documentation https://angular.io/docs/ts/latest/guide/server-communication.html#!#error-handling private handleError(error: any) { console.error(error); console.log(this.loginService); // <- always undefined return Observable.throw(error); } My problem is, that this.loginService is undefined although it has been injected in my class correctly. It is already used in other methods but seems not to be available in handleError . Could the way the method

Getting instance of service without constructor injection

一曲冷凌霜 提交于 2019-11-27 03:59:01
问题 I have a @Injectable service defined in bootstrap. I want to get the instance of the service without using constructor injection. I tried using ReflectiveInjector.resolveAndCreate but that seem to create a new instance. The reason I'm trying to do is I have a base component derived by many components. Now I need to access a service but I don't want to add it to the ctor because I don't want to inject the service on all of the derivative components. TLDR: I need a ServiceLocator.GetInstance<T>

ng2 - dynamically creating a component based on a template

大城市里の小女人 提交于 2019-11-27 01:13:49
I've been looking at the Angular 2 APIs for ComponentResolver and DynamicComponentResolver for creating dynamic components, but I have something different in mind than those APIs offer. Is there any way in NG2 to create a component based on a string of its class name? For example, Im building a configurable charts dashboard. Each user's layout is stored in the database, stating they want 2x line charts here, 3x bar charts there, etc. When I load this data as json it looks something like: user.charts = [ { type: 'LineChartComponent', position: ... } { type: 'BarChartComponent', position: ... }

How to call component method from service? (angular2)

狂风中的少年 提交于 2019-11-26 16:01:05
问题 I want to create service, which can interact with one component. All another components in my app, should be able to call this service, and this service should interact with this component. How to call component method from service? @Component({ selector:'component' }) export class Component{ function2(){ // How call it? } } From this servive? @Injectable() export class Service { callComponentsMethod() { //From this place?; } } 回答1: Interaction between components can be indeed achieved using

Testing - Can&#39;t resolve all parameters for (ClassName)

﹥>﹥吖頭↗ 提交于 2019-11-26 11:26:52
问题 Context I created an ApiService class to be able to handle our custom API queries, while using our own serializer + other features. ApiService \'s constructor signature is: constructor(metaManager: MetaManager, connector: ApiConnectorService, eventDispatcher: EventDispatcher); MetaManager is an injectable service that handles api\'s metadatas. ApiConnectorService is a service which is wrapping Http to add our custom headers and signature system. EventDispatcher is basically Symfony\'s event