【问题标题】:Img src ternary operator not displaying imageimg src 三元运算符不显示图像
【发布时间】:2023-03-28 18:56:01
【问题描述】:

我正在尝试根据条件对两个图像源使用角度三元运算符。我没有收到任何错误,但图像没有显示。

<img
          class="rounded-circle z-depth-2 mb-2"
          width="100"
          height="100"
          alt="profile picture"
          [attr.src]="user.user_profile_picture !== null ? 'http://localhost:8000/storage/profile_images/' +
            user.user_profile_picture.image : 'http://localhost:8000/storage/profile-picture.png'"
          data-holder-rendered="true"
        />
      </div>

我在这里犯了一个基本错误吗?

【问题讨论】:

  • 只使用[src],而不是[attr.src],它应该可以正常工作

标签: angular image src


【解决方案1】:

只尝试 [src],而不是 [attr.src]。

无论如何,由于您没有从您的角度项目(来自 src/asstes 或类似的)中获取图像,我不确定您之前是否必须“清理”此网址。

尝试并发表评论。

【讨论】:

  • 感谢您的回复,刚刚更新为 [src] 和相同的结果。
  • 仅用于测试:将 jpg 图像(重命名为 xxx.jpg)放入您的资产文件夹(例如:'/src/assets/xxx.jpg'),并将您的代码更改为: [src]="user.user_profile_picture != null ? 'assets/xxx.jpg' : 'assets/xxx.jpg'(现在,使用 != 而不是 !==)
  • 我刚刚在更新 [src] 后重建了应用程序,现在它决定可以工作了。
猜你喜欢
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 2021-04-27
  • 2015-07-26
  • 1970-01-01
  • 2020-09-17
  • 2016-09-27
  • 2014-06-23
相关资源
最近更新 更多