angular img标签使用err-src

在directive中定义组件,在ng-src错误时,调用err-src.

app.directive('errSrc',function() {
  return {
    link: function(scope, element, attrs) {
      if(attrs.src='undefined'){

        attrs.$set('src', attrs.errSrc);

      }
      element.bind('error', function() {
      if (attrs.src != attrs.errSrc) {
        attrs.$set('src', attrs.errSrc);
      }
    });
  }
}
});

posted @ 2016-08-27 16:05  芒果有毒  阅读(1865)  评论(0编辑  收藏  举报