Javascript 在ng repeat中检查项目是否为空,然后设置默认图像
我使用ng repeat绑定数据,但存在一个问题,即我使用{{obj.image}在图像列中显示的数据中存在图像 这是我的密码Javascript 在ng repeat中检查项目是否为空,然后设置默认图像,javascript,jquery,angularjs,image,null,Javascript,Jquery,Angularjs,Image,Null,我使用ng repeat绑定数据,但存在一个问题,即我使用{{obj.image}在图像列中显示的数据中存在图像 这是我的密码 <table class="table table-bordered table-hover"> <thead> <tr> <th>
<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>
高级专员。
标题
形象
类别
子类别
波斯顿
创建人
地位
{{$index+1}}
{{obj.CategoryName}
{{obj.subcategory name}}
{{obj.CreatedDate}
激活
激活
我想显示默认图像
在
当我的对象{{obj.Image}
为空时。
如何检查条件?您可以将控制器称为函数,以确定URL是什么
ng-href="{{ showImage(obj) }}"
代码
$scope.showImage = function(obj){
return obj.PageUrl ? '//'+ obj.PageUrl: '~/images/mail.png'
}
有多种方法可以做到这一点 您可以使用两个
img
标记并使用ng show
隐藏其中一个,具体取决于obj.image
:
<img ng-show="obj.Image" src="{{obj.Image}}">
<img ng-show="!obj.Image" src="default">
您的控制器中还可以有一个返回正确url的函数:
<img src="{{getImage(obj.Image)}}">
$scope.getImage(img) = function{
return img ? img : '~/images/mail.png';
};
$scope.getImage(img)=函数{
返回img?img:“~/images/mail.png”;
};
太好了您的解决方案非常完美