【问题标题】:Getting null while reading image through URL using Angular JS使用Angular JS通过URL读取图像时获取null
【发布时间】:2017-11-17 20:18:48
【问题描述】:

我有一个 URL,例如:w3schools 图像。当我尝试读取图像并计算其宽度和高度时,img.src=imgData 处为空。 这是我正在尝试的代码。为什么我无法从此 URL 读取图像。

var imgData = https://www.w3schools.com/w3css/img_fjords.jpg;
img.src = imgData;
img.onload = function () {
  $rootScope.width = this.width;
  $rootScope.height = this.height;
}

【问题讨论】:

标签: javascript html angularjs


【解决方案1】:

你也可以试试clientwidth和clientheight。

var imgData = https://www.w3schools.com/w3css/img_fjords.jpg;
    img.src = imgData;
    img.onload = function () {
       $rootScope.width = this.clientWidth;;
       $rootScope.height = this.clientHeight;
    }

【讨论】:

    【解决方案2】:

    您可以在图像元素上使用naturalHeightnaturalWidth。像这样:

    var imgData = https://www.w3schools.com/w3css/img_fjords.jpg;
    img.src = imgData;
    img.onload = function () {
       $rootScope.width = this.naturalWidth;
       $rootScope.height = this.naturalHeight;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2013-05-22
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 2012-04-16
      相关资源
      最近更新 更多