1

I've created an NgComponent and have declared it in the markup. Is it possible to query the component element and get the associated NgComponent instance?

4

1 回答 1

4

通常你不需要通过元素访问控制器。也就是说,有自省 APIngDirectives将返回给定节点的所有控制器的列表。但是 AFAIK 它仅用于调试

或者,如果您需要访问子组件/指令/控制器,请考虑以下模式:

<tabs>
  <pane title="Pane A">...</pane>
  <pane title="Pane B">...</pane>
  <pane title="Pane C">...</pane>
</tabs>
@NgComponent(
    selector: 'tabs',
    visibility: NgDirective.DIRECT_CHILDREN_VISIBILITY
)
class Tabs {
  List<Pane> _panes;

  registerPane(Pane pane) {
    _panes.add(pane);
  }
}

@NgComponent(
    selector: 'pane'
)
class Pane {
  @NgAttr('title')
  String title;

  Pane(Tabs tabs) {
    tabs.registerPane(this);
  }
}

当窗格被实例化时,它们向选项卡容器自我注册,随后选项卡容器可以访问窗格实例。

于 2014-01-24T14:07:02.857 回答