Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2:从属性指令获取对组件属性的引用_Angular_Typescript_Angular2 Directives - Fatal编程技术网

Angular 角度2:从属性指令获取对组件属性的引用

Angular 角度2:从属性指令获取对组件属性的引用,angular,typescript,angular2-directives,Angular,Typescript,Angular2 Directives,因此,我正在为网格(Ag网格)编写一组过滤器组件。我将在多个位置显示这些过滤器,因此我将为它们编写一个包装器,以便从不同的位置访问它们的值。目前,我正在实现一个过滤器抽屉,它将显示过滤器,并使用属性指令app drawer item获取对它们的引用: <app-filter-drawer #drawer [gridRef]="agGrid"> <app-text-filter app-drawer-item forColumn="AlertSid"></app

因此,我正在为网格(Ag网格)编写一组过滤器组件。我将在多个位置显示这些过滤器,因此我将为它们编写一个包装器,以便从不同的位置访问它们的值。目前,我正在实现一个过滤器抽屉,它将显示过滤器,并使用属性指令
app drawer item
获取对它们的引用:

<app-filter-drawer #drawer [gridRef]="agGrid">

  <app-text-filter app-drawer-item forColumn="AlertSid"></app-text-filter>

  <app-date-filter app-drawer-item forColumn="Name"></app-text-filter>

</app-filter-drawer>

如您所见,将有许多筛选器,因此将它们硬编码到
应用程序筛选器抽屉中是不明智的。我正在使用
app filter drawer
中的
@ContentChildren
来获取对它们各自指令的引用

是否可以从
app-drawer-item
指令获取对筛选器组件属性(如
app-text-filter
)的引用?我一直在玩弄
ElementRef
ViewContainer
,但我看不到直接引用组件实例的方法。我可以通过与主机的事件绑定获得它,但我只需要在用户单击“应用”按钮时访问这些值,而且每次更新值时发出事件似乎是浪费时间的。组件中还有其他方法,我最终需要从指令中调用这些方法


如果需要,我可以提供更多信息。谢谢

我找到了一个相对简单的解决方案。您只需使用指令中的
Injector
,手动注入Injector树中最接近的标记,该标记应为父标记:

export class DrawerItemDirective {

    constructor(@Inject(FilterDrawerComponent) public drawer) {}

    ngAfterViewInit() {
        const prop = this.drawer.propertyIWantToAccess;
        this.doStuff(prop);
    }
}

在@inject后面写“public”字,如下所示:“构造函数(@inject(FilterDrawerComponent)public drawer)”您不需要
@inject
组件,它可以由DI自动注入: