gpt4 book ai didi

javascript - observableArray 未定义

转载 作者:行者123 更新时间:2023-12-02 14:48:01 27 4
gpt4 key购买 nike

我在客户端开发环境中工作,必须遵守他们的编码标准。我有以下 JS 和 HTML。我的 observableArray 未定义。我无法让它工作。甚至 console.logs 也打印了正确的值。

请不要担心ko.applyBindings。已处理完毕。

我的 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);
var anotherObservableArray = ko.observableArray();

for (var i = 0; i < someArray.length; i++) {
var temp = {
"prodName": ko.observable(someArray[i])
};
anotherObservableArray.push(temp);
}
console.log(anotherObservableArray());
};
}
}
}
);

我的 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: anotherObservableArray"> // this doesnt work
<li>
<span data-bind="text: prodName"> </span>
</li>
</ul>
</div>

anotherObservableArray is not defined

最佳答案

您不会在声明的函数范围之外公开 anotherObservableArray。基本上您的代码采用以下格式:

{
onLoad: function (widget) {
widget.getDetails = function (prod) {
var anotherObservableArray = ko.observableArray();
// push some items into the array
console.log(anotherObservableArray());
};
}
}

您以某种方式需要在函数外部公开 anotherObservableArray 。例如:

{
onLoad: function (widget) {
widget.getDetails = function (prod) {
var anotherObservableArray = ko.observableArray();
// push some items into the array
console.log(anotherObservableArray());
this.anotherObservableArray = anotherObservableArray; // Expose it on the function
};
}
}

关于javascript - observableArray 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36430705/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com