我正在尝试数据绑定foreach但由于某种原因无法发现错误。 以下是片段: -
<div class="row">
<aside class="col-md-3">
<div class="sidenav">
<div>TEST</div>
<div class="list-group" data-bind="foreach: wikiLinks">
<a href="#" class="list-group-item list-group-item-action"
data-bind="text: articleText"></a>
</div>
</div>
</aside>
App.js文件是javascript文件,其中wikiLinks变量值是根据计算函数分配的。
this.wikiLinks = ko.computed(function () {
// load wikipedia data
var cityStr= "bothell";
var wikiUrl = 'http://en.wikipedia.org/w/api.php?action=opensearch&search=' + cityStr + '&format=json&callback=wikiCallback';
var wikiRequestTimeout = setTimeout(function(){
//$wikiElem.text("failed to get wikipedia resources");
}, 8000);
$.ajax({
url: wikiUrl,
dataType: "jsonp",
jsonp: "callback",
success: function( response ) {
var articleList = response[1];
articleList.forEach(function (article) {
self.wikiListArray.push(new wikiObj(article));
});
return self.wikiListArray();
// clearTimeout(wikiRequestTimeout);
}
});
}, this);}
var wikiObj = function (data) {
var self = this;
this.articleText = data;
this.url = 'http://en.wikipedia.org/wiki/' + data;
}