【问题标题】:Need to put Json link that contains img url in < img ng-src需要将包含img url的Json链接放入<img ng-src
【发布时间】:2016-04-27 08:34:56
【问题描述】:

我目前正在使用FirebaseIonic 构建一个移动应用程序,我必须使用firebase 的json 链接,该链接在我的html img ng-src 中包含一个图像url。

这是包含图片网址的Json link

我想将此链接放入 img 并获取此 json 链接 ("https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg") 中包含的 url 并显示图像,但我不知道该怎么做?

到目前为止,这是我在 html 中的内容。

<img src="'https://wellesleytesting.firebaseio.com/userProfile/userimg.json'" />

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • wellesleytesting.firebaseio.com/userProfile/userimg.json '"/> 我的意思是这就是我目前所拥有的
  • 该 JSON 链接现在指向 null,这是您应该在问题本身中包含 JSON(作为文本,没有屏幕截图)而不是链接到它的原因之一。

标签: javascript html json firebase


【解决方案1】:

您必须在您的 json 链接上发出http 请求,该请求会将图像链接作为响应发送给您。

$http.get("https://wellesleytesting.firebaseio.com/userProfile/userimg.json")
.then(function(response) {
    $scope.imgSrc = response.data;
    $scope.imgSrc = JSON.parse($scope.imgSrc)        
});

一旦你得到你的回应,你必须像这样解析它:

$scope.imgSrc = JSON.parse($scope.imgSrc) 

最后用ng-src属性将它绑定到你的html:

<img ng-src="{{imgSrc}}"/>

Demo

【讨论】:

  • 我明白了。非常感谢你的回复!我懂了。那么有没有办法直接在Html中做呢?因为我有 ng-repeat 会遍历图像列表,所以如果我需要做 http 请求,我需要将一个函数传递给 ng-src, .
  • 嗯不确定您的意思,但如果您想显示图像列表,您可以先获取所有图像来源,如我的答案。将其推送到$scope.listImages.push(imgSrc) 之类的列表中,然后用"ng-repeat = img in listImages" 显示它,我猜是这样的
  • 我的意思是我有

    {{event.displayName}}

    描述: {{event.description}}
  • 所以我问我不能直接获取图像 html,我应该传入一个函数来单独获取每个图像。 为您的困惑道歉,非常感谢您的帮助!!!!
  • for (var i = 0; i
猜你喜欢
  • 2016-04-30
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多