填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

ng.getOwningComponent

检索其视图中包含此 DOM 元素的组件实例。

Retrieves the component instance whose view contains the DOM element.

查看"说明"...

      
      ng.getOwningComponent<T>(elementOrDir: {} | Element): T | null
    
参数
elementOrDir {} | Element

要为其检索根组件的 DOM 元素、组件或指令实例。

DOM element, component or directive instance for which to retrieve the root components.

返回值

其视图中拥有 DOM 元素的组件实例;如果该元素不属于组件视图,则为 null。

T | null: Component instance whose view owns the DOM element or null if the element is not part of a component view.

说明

例如,如果 <child-comp><app-comp> 的模板中使用(即 <app-comp>ViewChild),在 <child-comp> 上调用 getOwningComponent 将返回 <app-comp>

For example, if <child-comp> is used in the template of <app-comp> (i.e. a ViewChild of <app-comp>), calling getOwningComponent on <child-comp> would return <app-comp>.