How can I select dynamic elements rendered by *ngIf

前端 未结 4 435
孤城傲影
孤城傲影 2021-01-18 01:48

As the code provided bellow. I tried to select a dynamic element generated by ngIf but failed.

I used two ways in total.

  1. ElementRef and querySelector
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-18 02:00

    Is not exactly the best but I deal with a similar situation using [ngClass] instead *ngIf. Just create a class with "display: none" and hide the elements when needed. The elements selected with @ViewChild can be accessed without problem. For now you can use this little "hack" while search for a better solution or more elegant one. ex.

    .hide-element{
        display: none;
    }
    
    
    

    if you are handling some async return you can just use async pipe

    
    

    Hope it helps.

提交回复
热议问题