我正在使用durandal创建一个web应用程序。我使用敲除将值从服务器绑定到页面。我有一个问题,在绑定敲除observableArray到页面,这是一个有点复杂的数组。
我的observableArray看起来

考虑到,
<ul data-bind="foreach: products">
<li><span data-bind='text: product' /></li>
</ul>没有错误也没有工作。
js代码。
define(['plugins/http', 'durandal/app', 'knockout'], function (http, app, ko) {
var system = require('durandal/system');
var vm = {
activate: activate,
attached: viewAttached,
products: ko.observableArray([])
};
return vm;
function activate() {
var that = this;
var pdts;
var recs;
var recipeJson = [];
http.get('http://***/Umbraco/Api/Products/GetAllProducts').then(function (response) {
pdts = response;
http.get('http://***/Umbraco/Api/Recipes/GetAllRecipes').then(function (response1) {
recs = response1;
$.each(pdts, function (i, item) {
var json = [];
$.each(recs, function (j, jtem) {
if (item.DocumentTypeId == jtem.BelongstoProduct) {
json.push(jtem);
}
});
jsonitem = {}
jsonitem["product"] = item.ProductName;
jsonitem["recipes"] = json;
recipeJson.push(jsonitem);
});
that.products = recipeJson;
return that.products;
});
});
}
function viewAttached(view) {
$("#accordion > li > div").click(function () {
if (false == $(this).next().is(':visible')) {
$('#accordion ul').slideUp(300);
}
$(this).next().slideToggle(300);
});
}
});帮帮忙,谢谢。
发布于 2014-06-11 04:35:00
当你写
that.products = recipeJson;您要用一个普通数组替换您的observableArray。你要么写
that.products(recipeJson);相反,或者直接将每一项推到可观察的数组中,而不是使用that.products.push(jsonItem)来遍历that.products.push(jsonItem)。
https://stackoverflow.com/questions/24154633
复制相似问题