I am trying to associate a simple array of integers with an unordered list with inputs, for example:
ViewModel
self.amounts = ko.observableArray(); for (var i=0; i<5; i++){ self.amounts.push(ko.observable(i)); }
HTML
<ul data-bind="foreach: amounts"> <li><input type="text" data-bind="value: $data"/></li> </ul>
This creates a list with correctly entered inputs with numbers 0-4. However, when I change the value of any of the inputs, the ViewModel does not receive new values.
If I use an array of simple objects as an observable array, for example:
[{amt: 0}, {amt: 1}, {amt: 2}]
and configure the HTML to bind the input to the "amt" property, and then changing the values ββon the inputs leads to changes in the ViewModel.
Is it possible to use an array of simple values ββand achieve two-way binding without using a workaround that modifies the original data model? If so, what am I doing wrong?
edit: this question is different from the one on which it was marked as a duplicate of Knockout JS - How to properly bind the observed array ..., this is about a special case of two-way binding for simple elements such as ints or strings.
source share