knockoutjs ko.observableArray绑定时不显示数据

时间:2013-01-18 16:44:36

标签: knockout.js

以下数据位于ko.observableArray 汽车中:(这是数组的转储)

[
  {
    "carId": 1,
    "carName": "Ford",
    "carStatus": "On-Hold",
    "carDescription": "This is the first car description."
  },
  {
    "carId": 1,
    "carName": "Toyota",
    "carStatus": "On-Hold",
    "carDescription": "This is the second car description."
  }
]

查看

<ul data-bind="foreach: cars()">
    <li>       
        <span data-bind="text: carDescription"></span>    
   </li>
</ul>

但是,没有输出任何内容,也没有显示列表项。

1 个答案:

答案 0 :(得分:2)

使用此官方教程http://learn.knockoutjs.com/#/?tutorial=collections

中的示例创建了小提琴

http://jsfiddle.net/vgYC7/

这就是ViewModel所需要的全部内容

function ViewModel() {
    var self = this;

    // Editable data
    self.cars = ko.observableArray([
      {
        "carId": 1,
        "carName": "Ford",
        "carStatus": "On-Hold",
        "carDescription": "This is the first car description."
      },
      {
        "carId": 1,
        "carName": "Toyota",
        "carStatus": "On-Hold",
        "carDescription": "This is the second car description."
      }
    ]);
}

ko.applyBindings(new ViewModel());