推荐答案这是一个非常简单的指令,用于观察加载图像的错误并替换 src.(Plunker)It's a pretty simple directive to watch for an error loading an image and to replace the src. (Plunker)HTML:<img ng-src="smiley.png" err-src="http://google.com/favicon.ico" />Javascript:Javascript:var app = angular.module("MyApp", []);app.directive('errSrc', function() { return { link: function(scope, element, attrs) { element.bind('error', function() { if (attrs.src != attrs.errSrc) { attrs.$set('src', attrs.errSrc); } }); } }});如果你想在 ngSrc 为空时显示错误图像,你可以添加这个 (Plunker):If you want to display the error image when ngSrc is blank you can add this (Plunker):attrs.$observe('ngSrc', function(value) { if (!value && attrs.errSrc) { attrs.$set('src', attrs.errSrc); }});问题是如果值为空,ngSrc 不会更新 src 属性.The problem is that ngSrc doesn't update the src attribute if the value is blank. 这篇关于如果 ngSrc 路径解析为 404,有没有办法回退到默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 09:13