[英]retrieve image url from object values - angular js
非常感谢Angular JS。
我正在研究一个项目,并且对此有解决方案,但是我想知道是否有更好的方法,如果可以的话,可以使用更多的“角度式”方法。
本质上,对于每个videoid ,它都会执行ajax请求,以定位该videoid的图像并将其放入src属性。
在纯JavaScript中,这没问题,我已经找到了一种解决方案,但它与角度方法有所不同。
我想知道是否可以做一些我在这里所做的事情。 (显然这还行不通)
标记
<ul class="storysContainer">
<li video-on-click ng-repeat="video in videos" id="video{{$index}}" >
<img ng-src="{{getVideoThumb()}}">
</li>
</ul>
JS
$scope.videos = [
{
'videoid': '1122345'
},
{
'videoid': '1134567'
},
{
'videoid': '2234456'
}
];
$scope.getVideoThumb = function() {
$.get("http://blahblah.com/id=" + url,function(data){
var urlMain = data.channel.item["media-group"]["media-thumbnail"]["@attributes"].url;
array.push(urlMain);
});
return array;
}
谢谢
编辑:
这是我想出的解决方案..不确定是否一定是最好的角度式方法,但它确实有效。
angular.forEach($scope.videos, function(data) {
var dataid = "http://blablah.com?id=" + data.videoid;
console.log(dataid);
$.get(dataid, function(img){
var urlMain = img.channel.item["media-group"]["media-thumbnail"]["@attributes"].url;
$('#thumb' + data.videoid).attr('src', urlMain);
//array.push(urlMain);
});
});
我会为videos
对象数组中的每个对象声明URL。 这样,您可以将值绑定在表示层中。
所以也许像
$scope.videos = [
{
'videoid': '1122345'
},
{
'videoid': '1134567'
},
{
'videoid': '2234456'
}
];
//modified this a little. I take it this call accepts the videoID as a parameter
//and returns the url for a single video?
//I wasn't sure what the 'array' variable you were using was.
//I am also using angular $http service to make this ajax call
$scope.getVideoThumb = function(videoID) {
$http.get("http://blahblah.com/id=" + videoID).success(function(data){
var urlMain = data.channel.item["media-group"]["media-thumbnail"]["@attributes"].url;
return urlMain;
});
}
//iterate through each object and assign it a 'URL' property to the result of 'getVideoThumb(videoID)'
for(var i = 0; i < $scope.videos.length; i++){
$scope.videos[i].URL = $scope.getVideoThumb($scope.videos[i].videoid);
}
现在在表示层中,我们可以将URL的值绑定到ng-src
<li video-on-click ng-repeat="video in videos" id="video{{$index}}" >
<img ng-src="{{video.URL}}">
</li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.