2
我试图从Google Feed API中拖动图像以显示在我正在构建的RSS阅读器中。我已经成功地拉出了publishedDate和contentSnippet,但似乎无法获得图像src。下面是我目前的饲料部分和控制器。在这里,我只是试图通过拉第一个图像来测试一个方法,但它不会返回任何东西。使用Angular从Google Feed API获取图像
feeds.html:
<div ng-repeat="feed in feeds | orderBy:'title'">
<span ng-repeat="item in feed.entries">
<h2><a href="{{item.link}}" target="_blank">{{item.title}}</a></h2>
<img src="{{firstImg}}" alt="">
<p>{{item.publishedDate}}</p>
<p>{{item.contentSnippet}}</p>
</span>
</div>
FeedCtrl.js:
var feeds = [];
angular.module('feedModule', [])
.factory('FeedLoader', function ($resource) {
return $resource('http://ajax.googleapis.com/ajax/services/feed/load', {}, {
fetch: { method: 'JSONP', params: {v: '1.0', callback: 'JSON_CALLBACK'} }
});
})
.service('FeedList', function ($rootScope, FeedLoader) {
this.get = function() {
var feedSources = [
{title: 'Breaking Muscle', url: 'http://breakingmuscle.com/feed/nowod.xml'},
{title: 'Precision Nutrition', url: 'http://www.precisionnutrition.com/feed'},
{title: 'Girls Gone Strong', url: 'http://www.girlsgonestrong.com/feed/'},
];
if (feeds.length === 0) {
for (var i=0; i<feedSources.length; i++) {
FeedLoader.fetch({q: feedSources[i].url, num: 10}, {}, function (data) {
var feed = data.responseData.feed;
feeds.push(feed);
});
}
}
return feeds;
};
})
.controller('FeedController', function ($scope, FeedList) {
$scope.feeds = FeedList.get();
$scope.$on('FeedList', function (event, data) {
$scope.feeds = data;
var findFirstImage = data[0].entries[0].content;
var firstImage = $(findFirstImage).find('img').eq(0).attr('src');
$scope.firstImg = firstImage;
});
});
有效的代码解决方案,但糟糕的答案 – charlietfl 2014-09-05 21:01:55