CanJs - Iterate through an array in mustache and create observables

540 views Asked by At
    ...    


option:{
  array: new can.List([1, 2, 3, 4, 5])
}
    ...

{{#each option.array}}
  <div>
    <label for="d{{@index}}"></label>
    <input id="d{{@index}}" can-value="option.array[{{@index}}]"/>
  </div>
{{/each}}

Above code generates 5 text boxes with correct id. But it does not create observables.

What is the correct format that I should provide to can-value attribute to make it an observable?

2

There are 2 answers

0
WHITECOLOR On

Try

<input id="d{{@index}}" can-value="{this}"/>
0
mehiel On

Quoting part of the Sections/Iteration documentation:

The {{.}} tag will reference the current item within the array during iteration (which is primarily used when the items in the array are primitives like strings and numbers).

It seems you want something like:

<input id="d{{@index}}" can-value="{{.}}"/>