我有一个运行Ionic的Cordova Android应用程序,我有一个脚本可以从多个URL获取RSS源。
为了确保它不仅仅存储本地存储中的最后一个feed条目,我创建了一个数组,我将每个RSS Feeds的条目推入其中,以便我可以显示所有feed中的所有entires。
这就是我设置数组的方式:
$scope.$apply(function(){
$scope.entryDB = [];
});
这是获取每个RSS Feed的代码的样子:
db.transaction(function(tx) {
tx.executeSql("SELECT (data) FROM note", [], function(tx,res){
for(var iii = 0; iii < res.rows.length; iii++){
alert(res.rows.item(iii).data);
$http.get("https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=" + res.rows.item(iii).data)
.success(function(data) {
$scope.rssTitle = data.responseData.feed.title;
$scope.rssUrl = data.responseData.feed.feedUrl;
$scope.rssSiteUrl = data.responseData.feed.link;
$scope.entries = data.responseData.feed.entries;
window.localStorage.setItem("entries", JSON.stringify(data.responseData.feed.entries));
$scope.entryDB.push(JSON.stringify(data.responseData.feed.entries));
alert($scope.entryDB)
})
.error(function(data) {
console.log("ERROR: " + data);
if(window.localStorage["entries"] !== undefined) {
$scope.entries = JSON.parse(window.localStorage["entries"]);
}
});
}
});
}, function(err){
alert("An error occured while displaying saved notes");
});
(res.rows.item(iii).data是每个Feed的网址)
奇怪的是,当我尝试使用ng-repeat显示条目时,如果我使用entryDB数组,它只将整个数组输出为HTML中的一个条目,但如果我使用localStorage“条目”,则通过正确迭代并显示多个卡片项目,效果非常好。
我已尝试对localStorage条目和entryDB条目执行alert(),它们看起来完全相同,但ng-repeat不会迭代entryDB条目。
这就是我试图显示这些内容的方式:
<ion-content ng-controller="FeedController" ng-init="init()" class="has-tabs-top">
<!-- // List of Cards // -->
<div class="list card" ng-repeat="entry in entryDB">
<!-- // Card Title // -->
<div class="item item-divider">{{entry.link}}</div>
<!-- // Card Contents // -->
<div class="item item-body" ng-click="browse(entry.link)">
<h2><b>{{entry.title}}</b></h2>
<p>{{entry.contentSnippet}}</p>
<p><a class="subdued">{{entry.publishedDate}}</a></p>
</div>
</div>
</ion-content>
如果我尝试只显示{{entry}},我可以看到它成功获取整个entryDB数组,但它不会迭代,只显示一个卡片项目,其中包含整个数组。
现在已经坚持了2天,我已经尝试在网上寻找了几个小时 - 我尝试在ng-repeat中添加'track by $ index',但这也没有用。
对于angularJS,我是一个完全新手,所以如果有人可以帮助我,我会很感激!
答案 0 :(得分:0)
更改此行$scope.entryDB.push(JSON.stringify(data.responseData.feed.entries));
到
$scope.entryDB.push(data.responseData.feed.entries);
答案 1 :(得分:0)
尝试使用angular.toJson而不是JSON.stringify