如何使用 angular 4 检查资产文件夹中是否存在图像 [英] how can i check if image exists in assets folder using angular 4

查看:20
本文介绍了如何使用 angular 4 检查资产文件夹中是否存在图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Angular 4 检查文件夹中是否存在图像.我有一个从服务器返回的图像名称列表.我需要的是检查该图像是否存在于资产文件夹中.任何帮助或想法都会很棒.

I need to check if a image exists in the folder using Angular 4.I have a list of image name that is returned from the server. What I need is to check if that image exists in the asset folder. Any help or idea would be great.

推荐答案

试试这个.在链接

// The "callback" argument is called with either true or false
// depending on whether the image at "url" exists or not.
function imageExists(url, callback) {
  var img = new Image();
  img.onload = function() { callback(true); };
  img.onerror = function() { callback(false); };
  img.src = url;
}

// Sample usage
var imageUrl = 'http://www.google.com/images/srpr/nav_logo14.png';
imageExists(imageUrl, function(exists) {
  console.log('RESULT: url=' + imageUrl + ', exists=' + exists);
});

我想警告你.小心使用它,由于它正在将图像加载到内存(如果存在),它可能会导致您一次检查更多图像的性能问题.

I would like to warn you. Becareful with it, It can run you to performance issue for checking more images at once due to it's loading images to memory if exists.

这篇关于如何使用 angular 4 检查资产文件夹中是否存在图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆