ionic 图片加载失败,显示默认图片代替

时间:2021-09-17 05:35:39

1.首先编写自定义指令

angular.module('starter.directives', [])
//当图片找不到事显示替代图片
.directive("errSrc", function() {
return {
link: function(scope, element, attrs) {
element.bind("error", function() {
if (attrs.src != attrs.errSrc) {
attrs.$set("src", attrs.errSrc);
}
});
}
}
})

2.引用

<img class="icon" ng-src="{{userData._avatar}}" ng-click="loadImage()" id="avatar" err-src="img/default_avatar.png">