In case I do:
data-bind="attr: {'id': $index}"
than IDs are 0, 1, 2... but if I do:
data-bind="attr: {'id': $index>0 ? 'choice'+$index : 'choice'}"
than ID is always 'choice', what do I do wrong?
It is Knockout v3.4.2
In case I do:
data-bind="attr: {'id': $index}"
than IDs are 0, 1, 2... but if I do:
data-bind="attr: {'id': $index>0 ? 'choice'+$index : 'choice'}"
than ID is always 'choice', what do I do wrong?
It is Knockout v3.4.2
$index
is actually an observable. In a simple binding, knockout will handle it automatically. In a more complex expression, it doesn't, you need to manually call it:
data-bind="attr: {'id': $index()>0 ? 'choice'+$index() : 'choice'}"
They all end up with just 'choice'
, because $index>0
will always evaluate to false
, since it's comparing the text representation of the $index
observable function with 0. This function starts with f
and "f" > 0
is false
.