gpt4 book ai didi

javascript - 检查 item 是否为 null 或 not in ng-repeat 然后设置默认图像

转载 作者:搜寻专家 更新时间:2023-11-01 05:28:44 24 4
gpt4 key购买 nike

我正在使用 ng-repeat 来绑定(bind)数据,但存在一个问题,即我使用 {{obj.Image}} 在图像列中显示的数据中有图像这是我的代码

<table class="table table-bordered table-hover">
<thead>
<tr>
<th>
Sr. no.
</th>

<th>
Title
</th>

<th>
Image
</th>
<th>
Category
</th>
<th>
SubCategory
</th>
<th>
PostedOn
</th>
<th>
Created By
</th>
<th>
Status
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="obj in PostedBlogList">
<td>{{$index+1}}</td>
<td><a ng-href="{{'//'+obj.PageUrl }}">{{obj.Title}}</a></td>
<td> <img style="width:90px"src="{{obj.Image}}" /></td>
<td>
{{obj.CategoryName}}
</td>
<td>
{{obj.SubCategoryName}}
</td>
<td>
{{obj.CreatedDate}}
</td>
<td>
<button class="btn btn-primary" type="submit" value="Activate">Activate</button>
</td>
<td>
<button class="btn btn-primary" type="submit" value="Activate">Activate</button>
</td>
</tr>
</tbody>
</table>

我想显示默认图片<img src="~/images/mail.png" alt="">

<td> <img style="width:90px"src="{{obj.Image}}" /></td>

当我的对象{{obj.Image}}一片空白。如何查看状态?

最佳答案

有多种方法可以做到这一点。

您可以使用两个 img 标签并使用 ng-show 根据 obj.image 隐藏其中之一:

<img ng-show="obj.Image" src="{{obj.Image}}">
<img ng-show="!obj.Image" src="default">

您还可以在 Controller 中使用一个函数来返回正确的 url:

<img src="{{getImage(obj.Image)}}">


$scope.getImage(img) = function{
return img ? img : '~/images/mail.png';
};

关于javascript - 检查 item 是否为 null 或 not in ng-repeat 然后设置默认图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41158752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com