Angular 2获取垫对话框容器nativeelement

时间:2018-06-21 09:13:04

标签: angular

我正在使用角度为2的材质对角线。现在,我想访问mat-dialog-container本机元素。

let nElem = this.dialogRef._containerInstance._elementRef.nativeElement;

当我尝试如上所述访问本机元素时,出现错误“ Property _elementRef”是私有的,只能在“ MatDialogContainer”类中访问

示例:-https://stackblitz.com/edit/angular-z5mgjl

2 个答案:

答案 0 :(得分:0)

以下内容在Angular 5项目中为我工作

 let nElem = dialogRef['_containerInstance']['_elementRef'].nativeElement;

希望它能起作用。

答案 1 :(得分:0)

获取“材质对话框”的本机元素的一种简单方法是使用ViewContainerRef。据我了解,在构造函数中使用ViewContainerRef时将获得此组件容器的引用。在您的情况下,它将是Mat对话框容器。

以下作品:

Worksheets("material pricing").Range("F" & myVLookupResult).Value = TextBox14.Value

在另一个有趣的注释上,这可用于轻松确定是在对话框内部还是外部进行了单击。

import { ViewContainerRef } from '@angular/core';


constructor(private _ViewContainerRef: ViewContainerRef) { 

}

ngOnInit() {
  let matDialogContainerRef = this._ViewContainerRef.element.nativeElement;
}