【问题标题】:How to get a child component's contentchildren in Angular 2?如何在 Angular 2 中获取子组件的 contentchildren?
【发布时间】:2016-04-27 15:17:45
【问题描述】:

我在 Angular 2 中遇到问题。我有一个主要组件,用于显示菜单。该组件有一个名为 Tabs 的子组件。在这个组件中,当在 Main 组件中单击菜单项时,会动态添加 Tab 组件。我在 Tabs 组件中使用 @ContentChildren 来获取所有 Tab 组件,这工作正常。但是,要使其正常工作,我需要将这些 contentchildren 进一步传递到 Main 组件。我该怎么做?我试过发射,但它似乎不起作用。

@ContentChildren(TabComponent) tabs: QueryList<TabComponent>;
@Output() public tabsArrayStart = new EventEmitter();
this.tabsArrayStart.emit(this.tabs);

【问题讨论】:

    标签: javascript typescript angular


    【解决方案1】:

    @Output() 只允许从父级传递

    (tabsArrayStart)="doSomething($event)
    

    我建议您改用共享服务。

    更多详情见https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-23
      • 2016-11-23
      • 2016-11-02
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 2016-07-15
      • 2017-02-15
      相关资源
      最近更新 更多