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

查看:113
本文介绍了我如何使用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天全站免登陆