The check item is null or not in ng-repeat, and then set the default image

I use ng-repeat to bind data, but there is a problem that there is an image in the data that I display in the image column using {{obj.Image}} Here is my code

<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>

I want to display the default image <img src="~/images/mail.png" alt="">

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

when my object {{obj.Image}}is null. How can I check the condition?

+4
source share
2 answers

There are several ways to do this.

You can use two tags imgand use ng-showto hide one of them depending on 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';
};
+3

, , URL.

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

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

Source: https://habr.com/ru/post/1663818/


All Articles