admin管理员组

文章数量:1410737

I have the following HTML:

<!-- ko.foreach: properties -->
<span data-bind="text: $data.Name"></span>
<span data-bind="text: $data.Age"></span>
<!-- /ko -->

And this javascript:

function MyViewModel() {
    var self = this;
    self.properties = ko.observableArray([
        {Name: "John", Age: 32},
        {Name: "Steve", Age: 23}
    ]);
}
ko.applyBindings(new MyViewModel());

Here's the jsfiddle /

Why doesn't the foreach work?

I have the following HTML:

<!-- ko.foreach: properties -->
<span data-bind="text: $data.Name"></span>
<span data-bind="text: $data.Age"></span>
<!-- /ko -->

And this javascript:

function MyViewModel() {
    var self = this;
    self.properties = ko.observableArray([
        {Name: "John", Age: 32},
        {Name: "Steve", Age: 23}
    ]);
}
ko.applyBindings(new MyViewModel());

Here's the jsfiddle http://jsfiddle/cFB5B/

Why doesn't the foreach work?

Share Improve this question asked Apr 29, 2013 at 15:12 Mihai DabisteMihai Dabiste 15712 bronze badges 1
  • what about including the lib into the fiddle? – bwoebi Commented Apr 29, 2013 at 15:15
Add a ment  | 

2 Answers 2

Reset to default 6

It's ko foreach, not ko.foreach when binding a foreach inside a HTML ment (Note 4):

<!-- ko foreach: properties -->
<span data-bind="text: Name"></span>
<span data-bind="text: Age"></span>
<!-- /ko -->

http://jsfiddle/cFB5B/1/

Also, you don't need to use the $data, but that wasn't causing the problem.

You have a type . to much in the foreach declaration:

Instead

<!-- ko.foreach: properties -->

it should be

<!-- ko foreach: properties -->

本文标签: javascriptKnockout foreach not workingStack Overflow