【问题标题】:Get Access to parent methods and variables through DOM - Angular 2+通过 DOM 访问父方法和变量 - Angular 2+
【发布时间】: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>

【问题讨论】:

标签: angular angular5


【解决方案1】:

是的,你只需要简单

import { ParentPage } from '../../pages/well-control/parent';
.
.
.
parent: ParentPage;
parent1: any;
.
.
.
constructor(...){
parent.method1();//you can use it anywhere
parent1 = parent.variable1;
}

【讨论】:

  • 子类不需要创建父类实例,可以通过调用父类方法来访问它们:this.method1()
  • 也许你的意思不是这个,但是,通过上面提到的答案,你可以轻松使用任何组件。
猜你喜欢
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2015-12-01
  • 1970-01-01
相关资源
最近更新 更多