我想创建一个通用指令,它将采用类类型进行规则验证,并根据类中的规则指令将显示或隐藏元素。在指令中手动获取从注入器的依赖关系
这是我迄今的尝试。
PLUNKER演示
myIf-Directive.ts
@Directive({
selector: '[myIf]'
})
export class MyIfDirective {
constructor(private _viewContainer: ViewContainerRef,
private _template: TemplateRef<Object>)
{ }
@Input() set myIf(rule: string) {
//rule class type will come as string
//how can I use string token to get dependency from injector?
//currently harcoded
//will the injector create new instance or pass on instance from parent?
let injector = ReflectiveInjector.resolveAndCreate([AdminOnly]);
let adminOnly : IRule = injector.get(AdminOnly);
let show = adminOnly.shouldShowElement();
show ? this.showItem() : this.hideItem();
}
private showItem() {
this._viewContainer.createEmbeddedView(this._template);
}
private hideItem() {
this._viewContainer.clear();
}
}
APP-component.ts
@Component({
selector: 'my-app',
template: `
<div *myIf="'AdminOnly'">
<h2>Hello {{name}}</h2>
</div>
`,
})
export class App {
name:string;
constructor() {
this.name = 'Angular2'
}
}
但我被困在两个地方:
- 我不断收到错误
No Provider for AuthService
- 我不知道我怎么可以从注射器使用类名作为字符串,而不是类型
任何建议,这是否是正确的方式来获得的依赖做到这一点,或者我错误的地方是高度赞赏。
*我不知道我怎么可以从注射器使用类名作为字符串,而不是类型得到的依赖*您可以“T。见http://stackoverflow.com/a/40063568/3731501 – estus
@estus所以,无论如何,我可以将'AdminOnly'类型传递给'my-if'指令而不是字符串? – lbrahim
我不确定这个指令应该如何工作。但是你必须传递组件构造函数,而不是一个字符串。如果你想把它作为一个字符串传递,应该像上面显示的那样使用组件和它们的字符串表示的映射。 – estus