Max*_*ali 5 javascript json google-feed-api knockout.js
我刚刚开始使用knockout.js,它在正常竞标中效果很好.我有observableArray的问题.
我想创建一个observableArray并为其分配来自Google Feed API的JSON数据.以下是JSON格式https://developers.google.com/feed/v1/devguide#resultJson
google.load("feeds", "1"); // Loads Google Feed API
function FeedViewModel()
{
// Data
var self = this;
self.allEntries = null;
// Example property, and it works
self.feedHead = ko.observable("BBC News");
var feed = new google.feeds.Feed("feeds.feedburner.com/BBCNews");
feed.setResultFormat(google.feeds.Feed.JSON_FORMAT);
feed.includeHistoricalEntries();
feed.setNumEntries(30);
// Loads feed results
feed.load(function (result) {
if (!result.error) {
self.allEntries = ko.observableArray(result.feed.entries);
// accessing the title from here is OK
alert(self.allEntries()[1].title);
}
});
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,从ViewModel访问数组是正常的,但是我需要使用foreach在浏览器中显示它:allEntries
<h2 data-bind="text: feedHead">Latest News</h2>
<!-- ko foreach:allEntries -->
<div class="lists">
<a href="#" data-bind="text: title"></a>
</div>
<!-- /ko -->
Run Code Online (Sandbox Code Playgroud)
但是ko foreach循环没有任何回报.可观察的feedHead是好的.
我也没有任何JS错误.任何帮助..
Kei*_*las 23
尝试声明(你有//数据)
self.allEntries = ko.observableArray([]);
Run Code Online (Sandbox Code Playgroud)
然后在负载......
self.allEntries(result.feed.entries);
Run Code Online (Sandbox Code Playgroud)