angular2-injection

Angular2 RC6 HttpModule manual injection

回眸只為那壹抹淺笑 提交于 2020-01-04 02:09:52
问题 I'm migrating a project from angular2 RC4 to RC6 and I have a custom Form Validator which needs Http . Before the migration I used the ReflectiveInjector with the HTTP_PROVIDERS , but with RC6 this is not possible anymore as HTTP_PROVIDERS is deprecated, respectively not present anymore. This is the static method in the Validator: static checkVat(control: FormControl) { let checkVatUrl = "http://localhost:8080/checkvat"; let injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);

Inject class array

我的未来我决定 提交于 2019-12-24 13:06:38
问题 I have a MetaManager class: @Injectable() export class MetaManager{ constructor(private handlers:Handler[]){ console.log(handlers); } } this class needs a Handler[] to register as handlers. Than, when I get some metadata, I loop in my handlers array to see which one can handler my meta entry. Having said that, problem is that I can't provide an array of class as config in main.ts, here is what I have tried (and what did not work): Using String entry (with a @Provide('HANDLERS') annotation in

Injecting different providers while loading components using ComponentResolver in Angular 2

对着背影说爱祢 提交于 2019-12-18 18:39:43
问题 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

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

萝らか妹 提交于 2019-12-17 19:17:34
问题 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

Angular 2 injector can't find Service when loading CustomExceptionHandler

谁说我不能喝 提交于 2019-12-11 07:56:08
问题 When trying to inject a service into my CustomExceptionHandler, the Angular injector cannot find the service. The error: Uncaught Can't resolve all parameters for CustomExceptionHandler: (?). The setup: CustomExceptionHandler import { ExceptionHandler } from '@angular/core'; import { ServiceA } from '../services/a.service'; export class CustomExceptionHandler extends ExceptionHandler { constructor(private serviceA: ServiceA) { super(null, null); } call(error, stackTrace = null, reason = null)

Angular 2 call service only on app initialization

谁说我不能喝 提交于 2019-12-10 16:02:48
问题 What I am trying to accomplish is to call external API only once per app initialization. I have a simple service, @Injectable() export class XService { url = "http://api.example.com" constructor(private _http:Http) { } callAnAPI(){ console.log('made an external request"); return this._http.get(url) .map(res=>res.json()); } } and two components, the main appComponent @Component({ selector: 'my-app', template: ` <div> Test </div> ` }) export class AppComponent { isLoading = true; results = [];

Angular 2 service not being injected into component

喜你入骨 提交于 2019-12-06 06:54:37
问题 I have a service defined in my Angular2 (2.0.0-beta.0) application. It's something like this: import {Injectable} from "angular2/core"; @Injectable() export class MyService { constructor() { } getSomething() { return 'something'; } } I have it listed in my bootstrap() function in my main app file so that it should be made available to my code in general: bootstrap(App, [MyService, SomeOtherService, ROUTER_DIRECTIVES[); Sometimes I can't use the service in a component, even though I have

Angular 2 service not being injected into component

风格不统一 提交于 2019-12-04 11:09:09
I have a service defined in my Angular2 (2.0.0-beta.0) application. It's something like this: import {Injectable} from "angular2/core"; @Injectable() export class MyService { constructor() { } getSomething() { return 'something'; } } I have it listed in my bootstrap() function in my main app file so that it should be made available to my code in general: bootstrap(App, [MyService, SomeOtherService, ROUTER_DIRECTIVES[); Sometimes I can't use the service in a component, even though I have something like myService:MyService in the component constructor() function, like this: import {MyService}

Angular 2 rc5 , unit testing issue with pipes that use injection

半世苍凉 提交于 2019-12-01 11:34:12
I am using angular2 rc 5, I have written a custom pipe that fetches value from a json. The custom pipe : literal.pipe.ts looks like : import {Pipe, PipeTransform, Inject} from '@angular/core'; import {MessageService} from '../service/message.service'; @Pipe({ name: 'literalString', pure: false }) export class LiteralPipe implements PipeTransform{ private messageBundle:any; private request:any; constructor(private _messageService: MessageService){ this._messageService = _messageService; this.messageBundle = {}; } transform(value:string, args:string[]):any { if(!this.request){ this.request =

Angular 2 rc5 , unit testing issue with pipes that use injection

给你一囗甜甜゛ 提交于 2019-12-01 07:35:03
问题 I am using angular2 rc 5, I have written a custom pipe that fetches value from a json. The custom pipe : literal.pipe.ts looks like : import {Pipe, PipeTransform, Inject} from '@angular/core'; import {MessageService} from '../service/message.service'; @Pipe({ name: 'literalString', pure: false }) export class LiteralPipe implements PipeTransform{ private messageBundle:any; private request:any; constructor(private _messageService: MessageService){ this._messageService = _messageService; this