0

Consider the following object:

[{
  key: 'value',
  amounts: [{key: 'value}]
}]

This array is tied to a observableArray, we will call this foo.

self.foo = ko.observableArray([{
  key: 'value',
  amounts: [{key: 'value}]
}])

Then we can do:

  <div id="something" data-bind="foreach: foo">
    <div class="somethingElse">
        // How do I get the amounts here?
    </div>
  </div>

So how would I, inside the element, get "amounts"? would it be:

data-bind="foreach: foo.amounts"

Seems logical to me ...

2
  • 1
    data-bind="foreach: amounts" Commented Dec 15, 2015 at 0:14
  • So you dont want internet points? How am I suppose to accept this? Also more explanation as to how it knows about nested arrays please. Commented Dec 15, 2015 at 0:15

1 Answer 1

1

When using the foreach binding, the inner HTML is repeated and each section is bound to an item from the array. It is bound as a "child" context" and so can directly access the properties of the array item.

Since the object bound to the inner HTML is

{
  key: 'value',
  amounts: [{key: 'value}]
}

You can use the properties directly

data-bind="foreach: amounts"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.