2017-03-27 35 views
1

我想创建一个自定义指令,将放置在DropDownList组件。在指令的构造函数中,我想获得主机DropDownList组件的引用。Kendo UI为角2访问组件从指令

这里是我的代码:

import { Directive, ElementRef } from '@angular/core'; 
 
import { DropDownListComponent } from '@progress/kendo-angular-dropdowns'; 
 

 
@Directive({ 
 
\t selector: '[trackDDLChanges]' 
 
}) 
 
export class TrackDDLChangesDirective { 
 
\t ddl: DropDownListComponent; 
 

 
\t constructor(elem: ElementRef) { 
 
\t \t this.ddl = elem.nativeElement; <-- how to get the reference of the component? 
 
\t \t 
 
\t } 
 
}

我找不到文档中的任何东西。 ElementRef给了我html元素,但我找不到一种方法将其转换为相应的组件。

在以前版本的剑道,我们可以这样做:

$(elem).data('kendoDropDownList')

去的小部件的引用。

这个版本有类似的东西吗?

谢谢。

回答

1

角DI将注入组件实例,如果适当的类型定义:

@Directive({ 
    selector: '[custom-behavior]' 
}) 
export class CustomDirective { 
    constructor(ddl: DropDownListComponent) { 
    console.log('test', ddl); 
    } 
} 

http://plnkr.co/edit/V5jJASqJ7NxToXIerp5z?p=preview

+1

辉煌。谢谢@George K – dpdragnev