我遇到的问题是,只有在刷新页面后,UI才会使用正确的数据进行更新。我来自这里observableArray is not defined
我的JS代码:
define(
['knockout'],
function (ko) {
"use strict";
return {
onLoad: function (widget) {
widget.getDetails= function (prod) {
var abc = prod.DetailsNumbers();
console.log(abc);
var someArray= [];
someArray= abc.split(',');
//console.log(someArray);
widget.anotherObservableArray = ko.observableArray();
for (var i = 0; i < someArray.length; i++) {
var temp = {
"prodName": ko.observable(someArray[i])
};
var myKoObservableArray = widget.anotherObservableArray;
myKoObservableArray.push(temp);
}
console.log(myKoObservableArray());
this.myKoObservableArray = myKoObservableArray;
};
}
}
}
);
我的HTML:
<div id="someId">
<table>
<tbody>
<tr>
<td>Button Here</td>
<td><button data-bind="click: getDetails(product())">Click me</button></td>
</tr>// this here is working fine
</tbody>
</table>
<ul data-bind="foreach: myKoObservableArray">
<li>
<span data-bind="text: prodName"> </span> //this changes only after a refresh
</li>
</ul>
</div>
data-bind="text: prodName"
仅在刷新页面后更改值。
答案 0 :(得分:2)
在onLoad中创建一次数组,然后只需更新它。不要每次都继续创建它。
define(
['knockout'],
function (ko) {
"use strict";
return {
onLoad: function (widget) {
widget.myKoObservableArray = ko.observableArray();
widget.getDetails= function (prod) {
var abc = prod.DetailsNumbers();
console.log(abc);
var someArray= [];
someArray= abc.split(',');
//console.log(someArray);
// empty the array
widget.myKoObservableArray([]);
for (var i = 0; i < someArray.length; i++) {
var temp = {
"prodName": ko.observable(someArray[i])
};
widget.myKoObservableArray.push(temp);
}
console.log(widget.myKoObservableArray());
};
}
}
}
);