当ng-src为空时,如何从项目文件夹上传默认图像?

时间:2015-04-21 17:49:18

标签: json angularjs

我有我的ng-src <img ng-src="{{incident._links.thumbnail.href}}">,phat来自JSON对象,如果phat为空,如何从<img ng-src="img/default.jpg">项目文件夹中显示默认图像

这是我的JSON

"_links": {
                "self": {
                    "href": "http://incidents-core/app_dev.php/incidents/5"
                },
                "attachments": {
                    "href": "http://incidents-core/app_dev.php/incidents/5/attachments"
                },
                "comments": {
                    "href": "http://incidents-core/app_dev.php/incidents/5/comments"
                },
                "thumbnail": {
                    "href": "http://incidents-core/app_dev.php/attachments/49/download"
                }
            }

这是空的

"_links": {
                "self": {
                    "href": "http://incidents-core/app_dev.php/incidents/5"
                },
                "attachments": {
                    "href": "http://incidents-core/app_dev.php/incidents/5/attachments"
                },
                "comments": {
                    "href": "http://incidents-core/app_dev.php/incidents/5/comments"
                }
            }

2 个答案:

答案 0 :(得分:2)

<img  ng-src="{{incident._links.thumbnail.href || 'img/default.jpg'}}">

答案 1 :(得分:0)

您可以使用ng-ifng-show/ng-hide来完成此操作。 提出这样的条件

 <img ng-src="{{incident._links.thumbnail.href}}" 
ng-show="incident._links.thumbnail.href !== undifined && incident._links.thumbnail.href !== ''>

    <img ng-src="img/default.jpg" 
ng-show="incident._links.thumbnail.href === undifined || incident._links.thumbnail.href === ''>