javascript - 使用 Angular 从 Google Feed API 获取图像

标签 javascript jquery json angularjs google-feed-api

我正在尝试从 Google Feed API 中提取图像以显示在我正在构建的 RSS 阅读器中。我已成功提取 publishedDate 和 contentSnippet,但似乎无法获取图像 src。下面是我当前的部分提要和 Controller 。在这里,我只是尝试通过拉取第一张图片来测试一种方法,但它没有返回任何内容。

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;
    });
});

最佳答案

请看这里:http://jsbin.com/xidik/1/edit或者那里 http://jsbin.com/xidik/3/edit正在为每个提要查找图像。

将 $q 服务添加到您的“FeedList”服务,然后在您的 FeedController 中迭代您的数据,当 promise 将被解决以查找图像时。

var app = angular.module('app', ['ngResource']);

var feeds = [];

app.factory('FeedLoader', function ($resource) {
    return $resource('http://ajax.googleapis.com/ajax/services/feed/load', {}, {
        fetch: { method: 'JSONP', params: {v: '1.0', callback: 'JSON_CALLBACK'} }
    });
});

app.service('FeedList', function ($rootScope, FeedLoader, $q) {
    this.get = function() {
        var deferred= $q.defer();
        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);
                    deferred.resolve(feeds);
                });
            }
        }

        return deferred.promise;
    };
});

app.controller('firstCtrl', function($scope, FeedList) {
    FeedList.get().then(function(data){
        $scope.feeds = data;

        angular.forEach(data[0].entries, function(value) {
            value.sapleImage =$(value.content).find('img').eq(0).attr('src');
        });       

    })
});

关于javascript - 使用 Angular 从 Google Feed API 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25693258/

相关文章:

json - 正则表达式模式的 Composer Json 验证错误 ^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{ 0,2})[a-z0-9]+)*$

php - 异步将数据放入动态生成的 div

javascript - NodeJS - 使用 node-fetch 发送 JSON 对象参数

javascript - 向禁用按钮添加类或样式

javascript - react 功能组件 : clearInterval() does not clear my interval

javascript - 添加到数组时,从 text() 返回的 jQuery 值会导致无法识别的表达式错误

jquery - 如何让两个 chrome 用户脚本仅使用一个 jQuery 实例?

javascript - 如何在 jquery 中添加减号?

javascript - Google chrome 中的奇怪行为(提交表单后,它会转到页面底部)

json - 将文件作为字符串传递给 Github Gist REST API