angular2-di

Angular 2 Di not working - cannot resolve all parameters for

眉间皱痕 提交于 2019-11-30 22:07:11
问题 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

Attempting to extend FormControlDirective to implement my own FormControl directive results in faulty binding

风格不统一 提交于 2019-11-30 16:16:38
问题 I'm trying to inverse the way forms controls are registering themselves onto a FormGroup , so that instead of having to @Component({..., template: `<input [formControl]="myControl"`} ... Or @Component({..., template: `<input [formControName]="myControlName"`} ... I could @Component({..., template: `<input myFormControl`} ... And have my directive create and add the FormControl for me. It is best explained with this Plunker. What doesn't seem to work is the binding the view to the form model,

Attempting to extend FormControlDirective to implement my own FormControl directive results in faulty binding

落花浮王杯 提交于 2019-11-30 16:01:47
I'm trying to inverse the way forms controls are registering themselves onto a FormGroup , so that instead of having to @Component({..., template: `<input [formControl]="myControl"`} ... Or @Component({..., template: `<input [formControName]="myControlName"`} ... I could @Component({..., template: `<input myFormControl`} ... And have my directive create and add the FormControl for me. It is best explained with this Plunker . What doesn't seem to work is the binding the view to the form model, as you can see, changing the the input does not change the form model value. Debugging it shows that

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,

How to use Dependency Injection (DI) correctly in Angular2?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 04:23:22
I have been trying to figure out how the (DI) Dependency Injection work in Angular2. I ran into lots of problem/issue every time when I tried to Inject a service/or class into my components. From different googled articles, I need to either use providers: [] in the Component configuration, or sometimes I need to use @Inject() in my constructor or inject directly in the bootstrap(app, [service]) ? I've also seen some articles want me to put @injectable decorator. For example: to inject Http, I only need to import{Http} and put Http in the providers, but for FormBuilder, I need to use @Inject()

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>

How to use Dependency Injection (DI) correctly in Angular2?

谁都会走 提交于 2019-11-26 11:09:44
问题 I have been trying to figure out how the (DI) Dependency Injection work in Angular2. I ran into lots of problem/issue every time when I tried to Inject a service/or class into my components. From different googled articles, I need to either use providers: [] in the Component configuration, or sometimes I need to use @Inject() in my constructor or inject directly in the bootstrap(app, [service]) ? I\'ve also seen some articles want me to put @injectable decorator. For example: to inject Http,

Angular DI Error - EXCEPTION: Can&#39;t resolve all parameters

倾然丶 夕夏残阳落幕 提交于 2019-11-26 00:46:00
问题 I\'ve built a basic app in Angular, but I have encountered a strange issue where I cannot inject a service into one of my components. It injects fine into any of the three other components I have created however. For starters, this is the service: import { Injectable } from \'@angular/core\'; @Injectable() export class MobileService { screenWidth: number; screenHeight: number; constructor() { this.screenWidth = window.outerWidth; this.screenHeight = window.outerHeight; window.addEventListener