【问题标题】:knockoutjs ko.observableArray not displaying data when boundknockoutjs ko.observableArray 绑定时不显示数据
【发布时间】:2013-01-20 00:50:54
【问题描述】:

以下数据在ko.observableArraycars中:(这是数组的转储)

[
  {
    "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>

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

【问题讨论】:

  • 你能不能把 JSFiddle 放在一起来显示你的问题?但是foreach: cars() 感觉很奇怪,因为 foreach 会自动展开 observable。试试&lt;ul data-bind="foreach: cars"&gt;
  • 虽然不推荐使用foreach: cars(),而不是foreach: cars,但它不会阻止渲染。这是一个显示它工作的小提琴:jsfiddle.net/jearles/UzC38

标签: knockout.js


【解决方案1】:

使用本官方教程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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-13
    • 2011-08-26
    • 2012-10-12
    • 1970-01-01
    • 2016-01-24
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多