简体   繁体   English

使用ViewChildren的子组件的访问指令

[英]Access directives of child components using ViewChildren

For accessing the directives in a component, I have tried Angular 2: Get reference to a directive used in a component but this doesn't work for directives in the child components of the component. 为了访问组件中的指令,我尝试了Angular 2:获取对组件中使用的指令的引用,但这不适用于组件的子组件中的指令。

The child components are created dynamically using ViewContainerRef. 子组件是使用ViewContainerRef动态创建的。

 @ViewChildren(ViewRefDirective) allMyDirectives; 

//This doesn't work for directives in the child components

I have multiple different child components, that's why I can't specify the name of a single child component in ViewChild for accessing its directives. 我有多个不同的子组件,这就是为什么我无法在ViewChild中指定单个子组件的名称来访问其指令的原因。

Directive 指示

@Directive({
  selector: '[view-ref-host]',
})
export class ViewRefDirective {
  constructor(public viewContainerRef: ViewContainerRef) {
   }
}

Parent component 父组件

<div>
  <div view-ref-host>Parent block
    <child-panel-one></child-panel-one>
    <child-panel-two></child-panel-two>
    <child-panel-three></child-panel-three>
  </div>
</div>

Child panel one component 子面板一组件

<div>
  <div view-ref-host>Child panel one
    <!-- html here -->
  </div>
</div>

Child panel two component 子面板两个组成部分

<div>
  <div view-ref-host>Child panel two
    <!-- html here -->
  </div>
</div>

Child panel three component 子面板三部分

<div>
  <div view-ref-host>Child panel three
    <!-- html here -->
  </div>
</div>

How can I use the ViewChild decorator to access all the directives in parent and child component? 如何使用ViewChild装饰器访问父级和子级组件中的所有指令?

You can define method in your parent component like: 您可以在父组件中定义方法,例如:

allMyDirectives: ViewRefDirective[] = [];

registerRef(ref: ViewRefDirective) {
  this.allMyDirectives.push(ref);
}

and register directive in directive's constructor: 并在指令的构造函数中注册指令:

@Directive({
  selector: '[view-ref-host]',
})
export class ViewRefDirective {
  constructor(
    public viewContainerRef: ViewContainerRef,
    @Optional() parent: AppComponent
  ) {
    if (parent) {
      parent.registerRef(this);
    }
  }
} 

Ng-run Example Ng运行示例

正确的实现方法是: @ViewChild("MyCustomDirective") allMyCustomDirectives;

you need to export the directive. 您需要导出指令。 Then only you can use it from parent or child. 然后只有您可以从父母或孩子那里使用它。 Then from parent bind it to a variable and access that variable using view child 然后从父级将其绑定到变量,并使用view child访问该变量

@Directive({
  selector: '[view-ref-host]',
  exportAs:'viewRefDirective'  
})
export class ViewRefDirective {
  constructor(public viewContainerRef: ViewContainerRef) {
   }
}

<div>
  <div #cdire=viewRefDirective view-ref-host>Child panel one
    <!-- html here -->
  </div>
</div>

 @ViewChildren('cdire') allMyDirectives; 

Update 更新资料

you can do this using QueryList . 您可以使用QueryList做到这QueryList in each Child, components add 在每个子代中,组件添加

@ViewChildren('cdire') children: QueryList<ViewRefDirective>;

In the parent, level add 在父级中,添加级别

@ViewChildren('cdire') allMyDirectives; 
@ViewChild(childPanelOneComponent) c1: childPanelOneComponent;
@ViewChild(childPanelTwoComponent) c2: childPanelTwoComponent;
@ViewChild(childPanelThreeComponent) c3: childPanelThreeComponent;

ngAfterViewInit() {
    this.c1.children.forEach((child) => child.showChildName());
    this.c2.children.forEach((child) => child.showChildName());
    this.c3.children.forEach((child) => child.showChildName());
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM