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