后备图像的 Angular 指令 [英] Angular directive for a fallback image
本文介绍了后备图像的 Angular 指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果单独服务器上的图像不存在,我想显示默认图像.是否有角度指令来完成此操作?
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?
推荐答案
不,但您可以创建一个.
No but you can create one.
HTML:
<img fallback-src="http://google.com/favicon.ico" ng-src="{{image}}"/>
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;
});
这篇关于后备图像的 Angular 指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文