【问题标题】:Unable to detect Canvas tag inside ngfor iteration - Angular无法在 ngfor 迭代中检测到 Canvas 标记 - Angular
【发布时间】:2022-01-20 14:40:34
【问题描述】:

我想在 ngfor 中使用 Canvas 标记,但每次它显示未定义或为空。我尝试了很多方法,但没有奏效。请帮帮我。

HTML:

<div class="col-sm-6 col-lg-4 pb-30 blog-parent" *ngFor="let item of blogList; let i = index;">
    <div class="blog-item">
        <a href="#">
            <div class="blog-item-figure">
                <canvas class="blog-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]);
    }

}

【问题讨论】:

    标签: javascript html angular typescript


    【解决方案1】:

    ngOnInit() 替换为ngAfterViewInit()

    【讨论】:

    • 成功了。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多