javascript - 在 HTML 上显示数据的 If Else 语法
问题描述
我正在尝试在图像上传递 alt 文本值,这是代码:
<img *ngIf="document.story_image_1 " [src]="document.story_image_1 " class="image-1" alt="{{document.image_alttxt | 'default alt text'}}" />
对于alt
属性,如果值为空,我希望它有一个默认的替代文本。请建议使用 if else 语句显示数据的正确语法是什么。
解决方案
您应该使用逻辑 OR 而不是按位 OR
alt="{{document.image_alttxt || 'default alt text'}}"
如果可以的话,您也可以使用可选链接 ( ?.
)document
undefined
alt="{{document?.image_alttxt || 'default alt text'}}"
推荐阅读
- devexpress - 在一个 xtrareport 中创建多个图表
- dataframe - 将 pyspark Dataframe 写入具有相同列数和一个额外的 autoIncrement 列的雪花表中
- multithreading - 重置代码
- reactjs - 如何为 React 应用程序配置 Rollup?
- sql - Oracle case 缺少关键字
- python - TypeError:blit 的目标位置无效
- amazon-web-services - 哪个 AWS 托管策略包含创建 EKS 集群的权限?
- javascript - 加载组件未在 React.js 表单组件中呈现?
- java - 使用 JPA 使用本机查询填充 DTO
- android - 如何从另一个列表中的列表中拯救项目