[英]Create ko.observableArray from JSON Object in knockout
我剛剛開始使用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);
}
});
}
在上面的示例中,從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 -->
但是ko foreach循環沒有任何回報。 可觀察的feedHead是好的。
我也沒有任何JS錯誤。 任何幫助..
嘗試聲明(你有//數據)
self.allEntries = ko.observableArray([]);
然后在負載......
self.allEntries(result.feed.entries);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.