我想创建一个observableArray。下面的代码打印“6:0”和“3:0”,即observableArrays为空。
我在Firefox 5和Chrome 11.0.696.68中测试了这个。
我做错了什么?
var myStringArray = new Array( "Bungle", "Bear", "George", "Hippo", "Zippy", "Unknown" );
var myObjectArray = new Array(
{ name: "Bungle", type: "Bear" },
{ name: "George", type: "Hippo" },
{ name: "Zippy", type: "Unknown" }
);
var viewStringModel = ko.observableArray( myStringArray );
var viewObjectModel = ko.observableArray( myObjectArray );
console.log(myStringArray.length + " : " + viewStringModel.length);
console.log(myObjectArray.length + " : " + viewObjectModel.length);
答案 0 :(得分:7)
改变:
console.log(myStringArray.length + " : " + viewStringModel.length);
console.log(myObjectArray.length + " : " + viewObjectModel.length);
到
console.log(myStringArray.length + " : " + viewStringModel().length);
console.log(myObjectArray.length + " : " + viewObjectModel().length);