javascript - 加载图像 Angular JS

标签 javascript html angularjs

我目前正在尝试将图像加载到 img 标签中。我的问题是图像的加载无法通过特定的 src url 完成。所以我创建了一个返回图像的函数。我当前正在以以下格式调用该函数,但未加载图像。请注意,我已经验证该函数确实返回一个值作为数组缓冲区。感谢您的帮助!

代码:

JS-

//use .factory Img object to return arraybuffer value
$scope.getImage = function(productid) {
      console.log(productid);
      par = {id: [productid]};
      Img.getImage(par).$promise.then(
        function(data){
          console.log("success:" + data); //I am able to see this result but not display in img tag
          return data;
        },
        function(data){
          console.log("error" + data);
        }
      );
    }

HTML-

<img ng-src="{{getimage(id)}}">

最佳答案

函数Img.getImage(par).$promise是异步的。您无法在内部返回值。

这不是最好的解决方案,但你可以这样做:

JS:

$scope.getImage = function(productid) {
  console.log(productid);
  par = {id: [productid]};
  Img.getImage(par).$promise.then(
    function(data){
      console.log("success:" + data); //I am able to see this result but not display in img tag
      scope.productionPicturePath = data;
      return data;
    },
    function(data){
      console.log("error" + data);
    }
  );
}

HTML:<img ng-src="{{productionPicturePath}}">

关于javascript - 加载图像 Angular JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47776456/

相关文章:

javascript - AngularJS : How to do ng-bind logic check?

angularjs - 将 Promise 转为 int

javascript - 根据输入生成随机 6 个字符

javascript - 如何动态创建并附加到 DIV?

javascript - 如何在 Typescript 中使用正则表达式进行电子邮件验证

javascript - 如何使用 Android 作为客户端访问 express.js/passport.js API?

javascript - 如何在用户想要选择一个选项后增加/减少 'select' 选项宽度

html - li ul li 太长,换行但没有缩进

html - 外部 CSS 不工作

javascript - angularjs无法将变量分配给html5视频src