Home > Software engineering >  Unable to detect Canvas tag inside ngfor iteration - Angular
Unable to detect Canvas tag inside ngfor iteration - Angular

Time:01-20

I want to work with Canvas tag inside ngfor but every times it showing undefined or null. I tried in many ways but not worked. Please help me.

HTML:

<div  *ngFor="let item of blogList; let i = index;">
    <div >
        <a href="#">
            <div >
                <canvas  id="canvas"></canvas>
            </div>
        </a>
    </div>
</div>

TS:
ngOnInit(): void {
    this.cropImage();
}
cropImage() {
    let canvas: any = document.getElementsByClassName("blog-canvas");
    for (let i = 0; i < this.blogList.length; i  ) {
        console.log("canvas: ", canvas[i]);
    }

}

CodePudding user response:

Replace ngOnInit() with ngAfterViewInit().

  •  Tags:  
  • Related