按日期排序rss新闻与角js
问题描述:
我指的是JavaScript中的stack overflow答案,但我无法在角js上工作。按日期排序rss新闻与角js
JavaScript的答案排序的RSS根据日期馈送(最新顶部)是如下
data.query.results.rss.sort(function(item1, item2) {
var d1 = new Date(item1.channel.item.pubDate)
var d2 = new Date(item2.channel.item.pubDate)
return d2.getTime() - d1.getTime()
})
我的角的js(离子框架)是如下:
$webServicesFactory.get(
"https://www.google.com/finance/company_news",
{},
{
output: "rss",
q: stock.e +":"+ stock.t
}
).then(
function success(xmlData) {
var x2js = new X2JS();
$globalFactory.personalStockNews = x2js.xml_str2json(xmlData).rss.channel.item;
$globalFactory.personalStockNews.sort(function(item1, item2) {
var d1 = new Date(item1.channel.item.pubDate);
var d2 = new Date(item2.channel.item.pubDate);
return d2.getTime() - d1.getTime();
})
console.info($globalFactory.personalStockNews);
$ionicLoading.hide();
$state.go("app.home");
},
function error(error) {
$globalFactory.personalStockNews = null;
$ionicLoading.hide();
$state.go("app.home");
}
);
},
function error(error) {
$ionicLoading.hide();
}
);
但是,它没有按预期排序。 我该如何将javascript修改为angular js?
您可以使用下面的rss订阅源 'https://www.google.com/finance/company_news?q=SGX:533&ei=EeiDWaGGMpXUuATxloPgAw&output=rss'根据日期对rss进行排序。
在HTML中,代码如下:
<a class="item item-icon-right" ng-if='showmore' ng-repeat="item in rssNews| orderBy: 'pubDate':true" ng-click="openitems(item.link)">
<h2>{{item.title}}</h2>
<h6>{{item.pubDate}}</h6>
<p {{story.description | htmlToPlaintext}}</p>
</div>
</a>
答
我觉得你的排序功能应该是这个样子:
function(item1, item2) {
var d1 = new Date(item1.channel.item.pubDate)
var d2 = new Date(item2.channel.item.pubDate)
if (d1>d2) return -1
if (d2>d1) return 1
return 0
}
当然它会帮助,如果你说想要的结果是什么在问题中。 :)
也删除您的ng重复orderby。这是重写你在控制器中进行的数组排序。
嗨。我想对rss链接进行排序,就像stackoverflow示例一样。但是,我无法对它进行角度js排序 – bkcollection
尝试你的代码,它仍然无法排序。也许你可以试试rss feed https://www.google.com/finance/company_news?q=SGX:533&ei=EeiDWaGGMpXUuATxloPgAw&output=rss – bkcollection
嗯,我发现有一件事是一个问题。我更新了我的答案。我怀疑你的原始排序功能实际上一旦你放弃ng重复功能。 –