Angular directive for a fallback image

前端 未结 4 910
走了就别回头了
走了就别回头了 2020-11-30 21:13

If an image on a separate server doesn\'t exist I\'d like to display a default image. Is there an angular directive to accomplish this?

4条回答
  •  不思量自难忘°
    2020-11-30 21:54

    No but you can create one.

    http://jsfiddle.net/FdKKf/

    HTML:

    
    

    JS:

    myApp.directive('fallbackSrc', function () {
      var fallbackSrc = {
        link: function postLink(scope, iElement, iAttrs) {
          iElement.bind('error', function() {
            angular.element(this).attr("src", iAttrs.fallbackSrc);
          });
        }
       }
       return fallbackSrc;
    });
    

提交回复
热议问题