angular2-di

How to add providers to Injector dynamically?

怎甘沉沦 提交于 2020-06-10 02:58:25
问题 Each component can specify new Provider s using its providers property in ComponentMetadata . Is there a way to specify providers dynamically from, say, constructor of the component? 回答1: I've done it in the bootstrap part. bootstrap(AppComponent,[ provide( RequestOptions, { useClass: DefaultRequestOptions } ), provide(Http, { useFactory: function(backend, defaultOptions) { return new Http(backend, defaultOptions); }, deps: [XHRBackend, RequestOptions]}), ]); I'm guessing it can be done in a

How to add providers to Injector dynamically?

南楼画角 提交于 2020-06-10 02:58:08
问题 Each component can specify new Provider s using its providers property in ComponentMetadata . Is there a way to specify providers dynamically from, say, constructor of the component? 回答1: I've done it in the bootstrap part. bootstrap(AppComponent,[ provide( RequestOptions, { useClass: DefaultRequestOptions } ), provide(Http, { useFactory: function(backend, defaultOptions) { return new Http(backend, defaultOptions); }, deps: [XHRBackend, RequestOptions]}), ]); I'm guessing it can be done in a

How to add providers to Injector dynamically?

本秂侑毒 提交于 2020-06-10 02:57:09
问题 Each component can specify new Provider s using its providers property in ComponentMetadata . Is there a way to specify providers dynamically from, say, constructor of the component? 回答1: I've done it in the bootstrap part. bootstrap(AppComponent,[ provide( RequestOptions, { useClass: DefaultRequestOptions } ), provide(Http, { useFactory: function(backend, defaultOptions) { return new Http(backend, defaultOptions); }, deps: [XHRBackend, RequestOptions]}), ]); I'm guessing it can be done in a

Dependency injection does not load all params

偶尔善良 提交于 2020-01-05 03:46:52
问题 I'm currently trying to create a custom XHRBackend for my angular2 app. I want to catch all http responses with a 401 http response code (unauthorized) to redirect the user on the login-page. But here comes my problem: the DI is not able to load the Router in my custom-backend . @Injectable() export class CustomXHRBackend extends XHRBackend { constructor( _browserXHR: BrowserXhr, _baseResponseOptions: ResponseOptions, _xsrfStrategy: XSRFStrategy, private router : Router) { super(_browserXHR,

Angular 2 “No provider for String!”

怎甘沉沦 提交于 2019-12-22 12:58:56
问题 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

How do I inject a parent component into a child component?

末鹿安然 提交于 2019-12-17 05:54:53
问题 I'm trying to inject a parent component into a child component. I thought this would be straightforward – simply specify/inject the parent component in the child's constructor() : constructor(private _parent:AppComponent) {} // child component constructor I get the following error: EXCEPTION: Cannot resolve all parameters for ChildComponent(?). Make sure they all have valid type or annotations. What am I missing? ChildComponent: import {Component} from 'angular2/core'; import {AppComponent}

How do I inject a parent component into a child component?

蓝咒 提交于 2019-12-17 05:54:36
问题 I'm trying to inject a parent component into a child component. I thought this would be straightforward – simply specify/inject the parent component in the child's constructor() : constructor(private _parent:AppComponent) {} // child component constructor I get the following error: EXCEPTION: Cannot resolve all parameters for ChildComponent(?). Make sure they all have valid type or annotations. What am I missing? ChildComponent: import {Component} from 'angular2/core'; import {AppComponent}

Injection of dynamic directive/component in Angular 2

醉酒当歌 提交于 2019-12-13 15:43:35
问题 I'm new to Angular 2 and thought I'd make an extendable master-detail flow to start learning. The 'master'-part is working, I can easily plugin the specific service I need to populate the list when bootstrapping the app. The problem lies in making the 'detail'-part extendable. Basically, the master-detail template has the following lines in the template: <div class="large-10 columns"> <detail [item]="masterItemActive" id="detail"></detail> </div> No, I could just load the Detail directive and

Angular 2 custom service not injected into a directive

微笑、不失礼 提交于 2019-12-12 19:43:06
问题 I have app.component like this. import {Component} from "angular2/core" import {HTTP_PROVIDERS} from "angular2/http" import {ChartDataService} from '../service/chartData.service' import {FilterService} from "../service/filter.service" @Component({ selector: 'app', template: ` <sidebar (filterChange)="onFilterChange($event)"></sidebar> <div dsChart></div> `, directives: [SidebarComponent, ChartDirective], providers: [HTTP_PROVIDERS, FilterService, ChartDataService], styleUrls: ['app/main/app

Angular 2: How Should We Be Handling Dependency Injection with ES6/ES7?

时光怂恿深爱的人放手 提交于 2019-12-12 19:22:51
问题 I have been working on a custom component within Angular 2 as I attempt to learn the ropes and make the switch while staying with ES6/ES7 due to job constraints. Say I have a component defined such as this: // Import Inject, Component and View constructor (for metadata) import {Inject, Injectable} from 'angular2/core'; import {Component, View} from 'angular2/core'; // Import NgClass directive import {NgClass} from 'angular2/common'; import { InjectMetadata } from 'angular2/core'; // #