【问题标题】:Check item is null or not in ng-repeat and then set the default image在 ng-repeat 中检查项目是否为空,然后设置默认图像
【发布时间】:2017-04-30 17:40:43
【问题描述】:

我正在使用 ng-repeat 绑定数据,但存在一个问题,即我使用 {{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>

我要显示默认图片&lt;img src="~/images/mail.png" alt=""&gt;

&lt;td&gt; &lt;img style="width:90px"src="{{obj.Image}}" /&gt;&lt;/td&gt;

当我的对象{{obj.Image}} 为空时。 我如何检查情况?

【问题讨论】:

    标签: javascript jquery angularjs image null


    【解决方案1】:

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

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

    <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';
    };
    

    【讨论】:

    • 您的解决方案非常完美
    【解决方案2】:

    您可以调用控制器函数来决定 URL 是什么。

    ng-href="{{ showImage(obj) }}"
    

    代码

    $scope.showImage = function(obj){
       return obj.PageUrl ? '//'+ obj.PageUrl:  '~/images/mail.png'
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 2021-03-31
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多