observableArray没有定义 [英] observableArray is not defined
问题描述
请不要担心 ko.applyBindings
。这是照顾。
我的JS代码:
define (
''knockout'],
函数(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>按钮在这里< / td>
< td>< button data-bind =click:getDetails(product())>点击我< /按钮>< / td>
< / tr> //这里的工作正常
< / tbody>
< / table>
< ul data-bind =foreach:anotherObservableArray> //这不起作用
< li>
< span data-bind =text:prodName> < /跨度>
< / li>
< / ul>
< / div>
anotherObservableArray未定义
blockquote>
解决方案您不会在您声明的函数范围之外公开
anotherObservableArray
。基本上你的代码是这样的格式:
{
onLoad:function(widget){
widget .getDetails = function(prod){
var anotherObservableArray = ko.observableArray();
//将一些项目推入数组
console.log(anotherObservableArray());
};
你需要暴露
anotherObservableArray
在函数之外。例如:
{
onLoad:function(widget){
widget.getDetails = function(prod ){
var anotherObservableArray = ko.observableArray();
//将一些项目推入数组
console.log(anotherObservableArray());
this.anotherObservableArray = anotherObservableArray; //暴露在函数
};
}
}
I am working in a client development environment and have to adhere to their coding standards. I have the following JS and HTML. My observableArray is coming as not defined. I am not able to get it working. Even the console.logs are printing the correct values.
Please don't worry about
ko.applyBindings
. It is taken care of.My JS Code:
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()); }; } } } );
My HTML Code:
<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
解决方案You don't expose
anotherObservableArray
outside the function scope you declare it in. Basically your code is of this format:{ onLoad: function (widget) { widget.getDetails = function (prod) { var anotherObservableArray = ko.observableArray(); // push some items into the array console.log(anotherObservableArray()); }; } }
You somehow need to expose the
anotherObservableArray
outside the function. For example:{ 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 }; } }
这篇关于observableArray没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文