为什么仅使用私有访问说明符来实例化构造函数中的提供程序?是否有任何特定原因仅使用私有访问说明符?
私人注射
constructor(private service: InjectedService)
Run Code Online (Sandbox Code Playgroud)
公众注射
constructor(service: InjectedService)
Run Code Online (Sandbox Code Playgroud) 有没有办法通过服务名称获得角度为2的可注射服务的实例?
例如,在Angular 1中你可以写:
var service = $injector.get('ServiceName');
Run Code Online (Sandbox Code Playgroud)
并且service变量将获得服务的实例.
我真的很感谢你的帮助!
有没有一种简单的方法将输入绑定注入提供程序工厂的deps数组?下面显然不起作用.
const myServiceFactory = (object: any) => {
//...
};
@Component({
// ...
inputs: ['object'],
providers: [
{
provide: Object,
useValue: object,
},
{
provide: MyService,
useFactory: myServiceFactory,
deps: [Object]
}
]
})
Run Code Online (Sandbox Code Playgroud) 我正在将一个项目从angular2 RC4迁移到RC6,我有一个需要的自定义Form Validator Http.在迁移之前我使用了ReflectiveInjectorwith HTTP_PROVIDERS,但是使用了RC6,这已经不再可能了,因为HTTP_PROVIDERS已经弃用了,分别不再存在了.这是Validator中的静态方法:
static checkVat(control: FormControl) {
let checkVatUrl = "http://localhost:8080/checkvat";
let injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
let http = injector.get(Http);
let authHttp = new AuthHttp(new AuthConfig(), http);
if (control.value === "") {
return new Observable((obs: any) => {
obs.next(null);
obs.complete();
});
} else {
return authHttp.get(checkVatUrl + "/" + control.value)
.map((data: Response) => {
if (data.json().valid) {
return null;
} else {
let reason = "isNotValidVat";
return {[reason]: true};
}
})
.catch(function (e) …Run Code Online (Sandbox Code Playgroud) 我看到可以导入两种不同的方式模块.
大多数导入看起来像 'import {<something>}
(即import { Component } from '@angular/core';)
其他导入如'import * as <something>(即import * as _ from "lodash";)
根据我的理解,当使用typings(即typings install lodash=npm --save)而不是Angular2模块将vanilla js模块导入项目时,使用后一种方法导入是正确的吗?
如果我的假设是正确的,你是否以相同的方式使用两个导入的类/模块(即当你声明它们在Components类中使用时)?
javascript import angular2-injection typescript-typings angular
我正在尝试做的事情:
Plnkr -> http://plnkr.co/edit/Do4qYfDLtarRQQEBphW3?p=preview
在查看 angular.io 文档时,我发现“Injector”可用于获取构造函数中的父组件
constructor(private el: ElementRef, private hostComponent:Injector){
el.nativeElement.draggable = 'true';
}
Run Code Online (Sandbox Code Playgroud)
在这样做时,我得到了 Injector Object。据我所知,我应该使用
this.hostComponent.get(INJECTORTOKEN)
Run Code Online (Sandbox Code Playgroud)
我难以理解的问题是,Angular 中提供的示例假设您知道要在令牌中提供的 Component 类型。IE:
this.hostComponent.get(Image);
this.hostComponent.get(Box);
Run Code Online (Sandbox Code Playgroud)
在 pnkr 示例中,我的模板中有两个组件
<div>
<h2>Hello {{name}}</h2>
<my-image></my-image> <!-- Uses the "My Directive" -->
<my-box></my-box> <!-- Uses the "My Directive" -->
</div>
Run Code Online (Sandbox Code Playgroud)
我的问题是,在“mydirective.ts”中。当我不知道父组件是“my-image”还是“my-box”组件时,如何利用“injector.get()”。
在提供的示例中,指令被触发“ondrag()”。查看控制台,获取日志消息。
非常感谢任何帮助。
非常感谢你。
我有一个MetaManager类:
@Injectable()
export class MetaManager{
constructor(private handlers:Handler[]){
console.log(handlers);
}
}
Run Code Online (Sandbox Code Playgroud)
这个类需要Handler[]注册为处理程序.当我得到一些元数据时,我在我的处理程序数组中循环以查看哪个可以处理我的元条目.
话虽如此,问题是我无法在main.ts中提供一个类数组作为配置,这是我尝试过的(以及什么不起作用):
使用String条目(@Provide('HANDLERS')在MetaManager中使用注释:
`bootstrap(AppComponent, [MetaManager,provide('HANDLERS', {useValue: [DebugHandler]}) ]);`
Run Code Online (Sandbox Code Playgroud)
使用接口提供处理程序:
export const HANDLERS: HandlerConfig = {handlers: [<Handler>DebugHandler]};
bootstrap(AppComponent, [MetaManager, provide(HandlerConfig, {useValue: HANDLERS}) ]);
Run Code Online (Sandbox Code Playgroud)
使用Handler[]类提供者:
bootstrap(AppComponent, [MetaManager, provide(Handler[], [DebugHandler])]);
Run Code Online (Sandbox Code Playgroud)
我想提供一个类数组,因为MetaManager将来需要有多个Handler,通知,错误等...
编辑:
使用多提供程序给我一个No provider for Array! (ApiService -> MetaManager -> Array)错误:
元manager.ts:
@Injectable()
export class MetaManager{
constructor(private handlers:Handler[]){
console.log(handlers);
}
}
Run Code Online (Sandbox Code Playgroud)
main.ts:
bootstrap(AppComponent,
[ MetaManager,
provide(Handler, {useClass: DebugHandler, multi: true}),
provide(Handler, {useClass: NotificationHandler, …Run Code Online (Sandbox Code Playgroud)