如何使用angular 4在*ngIf中获取Dom元素

如何使用angular 4在*ngIf中获取Dom元素,angular,Angular,在我的页面中,我有4个步骤使用*ngIf在每个div之间切换,就像 <div *ngIf="step===1"></div> <div *ngIf="step===2"></div> <div *ngIf="step===3"></div> <div *ngIf="step===4"></div> 我在步骤2中有一个canvas元素,可以使用步骤1中的数据进行渲染。 我的问题是,当用户单击“下一

在我的页面中,我有4个步骤使用
*ngIf
在每个
div
之间切换,就像

<div *ngIf="step===1"></div>
<div *ngIf="step===2"></div>
<div *ngIf="step===3"></div>
<div *ngIf="step===4"></div>

我在步骤2中有一个
canvas
元素,可以使用步骤1中的数据进行渲染。 我的问题是,当用户单击“下一步”按钮时,我更改了
step=2
,并且我需要渲染
canvas
。但正如我们所知,
canvas
元素在呈现角度的UI之前是不存在的

那么,有没有一种方法可以在角度渲染后获得
画布
元素呢


虽然我知道
[hidden]
而不是
*ngIf
可以做到这一点,但是有没有
*ngIf
的方法<代码>设置超时不在我的考虑范围之内。

不是100%确定这是否有效,但值得一试:

 <div *ngIf="...">
   <canvas #canvas>

画布是动态加载的还是隐藏的?@wannadream just hidden当你可以使用javascript操作它时,我不太明白。你能详细解释一下吗?现在有人给了你一个细节。看一看。
constructor(private cdRef:ChangeDetectorRef) {}

@ViewChild('cd') canvas:ElementRef;

step = 1;
nextStep() {
  this.step++;
  this.cdRef.detectChanges();
  console.log(this.canvas.nativeElement);
}