angular2-di

Pattern to enable customization through DI

。_饼干妹妹 提交于 2019-12-12 03:31:00
问题 I'm looking at the implementation of in-memory-web-api and there is the following code: @Injectable() export class InMemoryBackendService { protected config: InMemoryBackendConfigArgs = new InMemoryBackendConfig(); ^^^^^^ ... constructor( @Inject(InMemoryBackendConfig) @Optional() config: InMemoryBackendConfigArgs ^^^^^^ ) { ... As I understand the pattern is the following: Defined class property and instantiate a dependency without using DI Optionally inject dependency If a user provides

Why use `deps` property in DI

依然范特西╮ 提交于 2019-12-10 11:37:18
问题 Here is the code snippet from angular.io: { provide: RUNNERS_UP, useFactory: runnersUpFactory(2), deps: [Hero, HeroService] } ... export function runnersUpFactory(take: number) { return (winner: Hero, heroService: HeroService): string => { /* ... */ }; }; My question is why deps property is used here? What are the general cases for using deps ? 回答1: This is a way to tell Angular dependency injections what dependencies it needs to inject to the factory function returned by runnersUpFactory .

How do you inject an angular2 service into a unit test? (RC3)

六眼飞鱼酱① 提交于 2019-12-09 18:30:14
问题 I am using RC3 . I am implementing the new Angular2 router as documented here: https://angular.io/docs/ts/latest/guide/router.html Everything works fine but I am having problem in unit testing. Specifically, I cannot inject Angular2 services into my unit tests. My relevant component code is: import {Component} from '@angular/core'; import {ActivatedRoute} from '@angular/router'; @Component({ templateUrl: ... styleUrls: ... }) export class Route1DetailComponent { constructor(private route

Angular. Router DI not working when using APP_INITIALIZER

二次信任 提交于 2019-12-09 18:20:46
问题 I'm preloading app configuration from server with APP_INITIALIZER in following way, AppModule : providers: [ ConfigService, { provide: APP_INITIALIZER, useFactory: configServiceFactory, deps: [ConfigService], multi: true } ], Then, ApiService injected manually from ConfigService : @Injectable() export class ConfigService { private api: ApiService; public constructor( private injector: Injector ) { // Avoid cyclid dependencies, inject manually: this.api = injector.get(ApiService); } And

Angular 2 “No provider for String!”

余生颓废 提交于 2019-12-06 06:00:32
I'm trying to create a generalized data service in Angular 2, and I'm encountering a strange error. Essentially, I'm creating an HTTP service whose methods take in part of the api url so that I can use it for multiple cases. For example, I would like to pass in 'projects/' and join it with 'api/' to get 'api/projects/' which I could then use in the http calls. My current dataService.ts: import { Injectable } from '@angular/core'; import { Http, Response, Headers } from '@angular/http'; import 'rxjs/add/operator/map' import { Observable } from 'rxjs/Observable'; import { Configuration } from '.

Angular2 passing function as component input is not working

▼魔方 西西 提交于 2019-12-04 18:31:43
问题 I've a component that takes function as input. I've passed this function from parent. Though the function is called, the function is not able to access the dependencies of the instance on which this function is declared. Here is the component @Component({ selector: 'custom-element', template: ` {{val}} ` }) export class CustomElement { @Input() valFn: () => string; get val(): string { return this.valFn(); } } Here is how the component is used @Injectable() export class CustomService { getVal(

How do you inject an angular2 service into a unit test? (RC3)

梦想与她 提交于 2019-12-04 08:47:46
I am using RC3 . I am implementing the new Angular2 router as documented here: https://angular.io/docs/ts/latest/guide/router.html Everything works fine but I am having problem in unit testing. Specifically, I cannot inject Angular2 services into my unit tests. My relevant component code is: import {Component} from '@angular/core'; import {ActivatedRoute} from '@angular/router'; @Component({ templateUrl: ... styleUrls: ... }) export class Route1DetailComponent { constructor(private route:ActivatedRoute) { console.log(route); } } my unit test looks like: import { expect, it, iit, xit, describe,

Angular. Router DI not working when using APP_INITIALIZER

懵懂的女人 提交于 2019-12-04 08:30:29
I'm preloading app configuration from server with APP_INITIALIZER in following way, AppModule : providers: [ ConfigService, { provide: APP_INITIALIZER, useFactory: configServiceFactory, deps: [ConfigService], multi: true } ], Then, ApiService injected manually from ConfigService : @Injectable() export class ConfigService { private api: ApiService; public constructor( private injector: Injector ) { // Avoid cyclid dependencies, inject manually: this.api = injector.get(ApiService); } And finally router is undefined when injected in ApiService import { Http, Headers, RequestOptionsArgs, Response

Angular2: inject server side config into service

白昼怎懂夜的黑 提交于 2019-12-02 12:14:38
问题 I'm using Angular 2.0.0 with TypeScript in ASP.NET Core. My goal is to create AppConfig service in my app, based on server-side variables. With a help from few other answers, I was able to create following code: Index.cshtml <app> <i class="fa fa-spin fa-5x fa-spinner"></i> </app> <script> System.import('/app/main').then((m) => { var config = { apiUrl: @options.ApiServerUrl }; m.RunApplication(config); }, console.error.bind(console)); </script> app.config.ts import { Injectable } from "

Angular 2 Di not working - cannot resolve all parameters for

烂漫一生 提交于 2019-12-01 02:28:34
I've created a simple Hello World app that works fine. But when I want to add a "Service" just a simple Di I got the following errors: angular2.dev.js:23877 EXCEPTION: Cannot resolve all parameters for 'AppComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AppComponent' is decorated with Injectable. angular2-polyfills.js:469 Unhandled Promise rejection: Cannot resolve all parameters for 'AppComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AppComponent' is