创建的observableArray始终为空

时间:2011-06-30 16:01:47

标签: javascript knockout.js

我想创建一个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);

1 个答案:

答案 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);