繁体   English   中英

Angular img tag src 条件

[英]Angular img tag src conditional

我有 img1.png、img2.png、img3.png 和 img4.png,如何根据条件在 src 中添加这些图像之一

<img src ="if a===1 then img1.png if a ===2 then img2.png if a === 3 then img3.png if a ===4 then img4.png>

最好的方法是在您的 component.ts 文件中动态设置图像源。

你的模板

<img [src]="image">

在您的组件中

get image() { return...add your logic to figure out what image to show}

在 function 中运行条件,并在 src= 上使用插值,例如:

 src={{imageVariable}} in the ts part call useImagae(){ if(a === 1) this.imageVariable = "img1.png" }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM