【发布时间】:2018-11-08 01:23:06
【问题描述】:
您好,有没有办法通过 DOM 访问上层组件及其变量和方法?
在我的示例中,Angular 代码是动态生成的,并且可能有不同的组件。但是 Child 确实知道父 DOM 组件有哪些方法和变量。
有没有办法访问方法和变量?
例如:https://stackblitz.com/edit/angular-dhpnf8
<hello name="grandparent1">
<some-other-component name="parent1">
<div>
<hello name="grandchild1">
</hello>
<hello name="grandchild2">
</hello>
</div>
</some-other-component>
</hello>
【问题讨论】:
-
你可以举一些你想做的例子吗?在你的例子中?
-
我做了一个演示应用。 stackblitz.com/edit/angular-dhpnf8
-
不好的方法是使用 ViewContainerRef,检查这个stackblitz.com/edit/angular-enfnsm。