Javascript ng src path在第一个抛出404时未选择备选图像url
我试图在我的网页中显示一个图像,但该图像的url没有返回404。我用的是AngularJS 我无法使用ngIf检查url,因为它会检查其是否为null。当url返回404或不返回404时,我将显示一些内容,但如果是404,我将显示此内容 如果返回404,是否有任何方法不显示图像,并在图像存在时显示图像。 HTML代码Javascript ng src path在第一个抛出404时未选择备选图像url,javascript,angularjs,Javascript,Angularjs,我试图在我的网页中显示一个图像,但该图像的url没有返回404。我用的是AngularJS 我无法使用ngIf检查url,因为它会检查其是否为null。当url返回404或不返回404时,我将显示一些内容,但如果是404,我将显示此内容 如果返回404,是否有任何方法不显示图像,并在图像存在时显示图像。 HTML代码 <img class="menu" ng-if="$ctrl.companyLogo" ng-src="{{ $ctrl.companyLogo}}"> 当comp
<img class="menu" ng-if="$ctrl.companyLogo" ng-src="{{ $ctrl.companyLogo}}">
当companyLogo返回404时,不显示默认徽标,而是不显示任何图像。借助图像对象,您可以检查图像路径是否返回404或实际图像
var given_src = 'http://idontexist.com/404.png';
var image = new Image();
image.src = given_src;
image.onerror = function(){console.log('doesnt exist or smth ');}
image.onload = function() {console.log('exists or smth');};
这是一个提琴,您还应该注意,$scope。$apply在提琴中是一个黑客解决方案,您应该执行此功能的承诺。这不会按您预期的方式工作。它不是检查目的地是否存在,而是检查
$ctrl.companyLogo
是否有一个值,它会这样做;404发生在后面。@Claies你知道关于我修改过的问题吗。
var given_src = 'http://idontexist.com/404.png';
var image = new Image();
image.src = given_src;
image.onerror = function(){console.log('doesnt exist or smth ');}
image.onload = function() {console.log('exists or smth');};