Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I bind a ko.observableArray of strings?
    primarykey
    data
    text
    <p>I'm trying to bind a ko.observableArray of strings to a template, but I'm unable to get the template to pick up changes in the strings inside the array.</p> <p>If I bind a set of objects instead of a set of strings, I get updates to the JSON, but they don't trigger anything until I actually change the first, non-array value. I'd prefer to find an array of strings, however, as I will be able to post the data model directly back to the server without any post-processing.</p> <p>How can I get the updates to my arrays of strings to trigger, and how can I ensure that they correctly trigger changes without having to update a non-array value? </p> <p>If it's not possible to bind to an observable array of strings, how I can get the events to trigger when updating the objects inside of the observable array?</p> <p>See the example here: <a href="http://jsfiddle.net/gcEHC/2/" rel="noreferrer">http://jsfiddle.net/gcEHC/2/</a></p> <p>In this example, array3's data will be reflected in the model when value is changed, but changes to array1 and array2's data will never show up.</p> <p>JS:</p> <pre><code>var ViewModel = function() { this.value = ko.observable("hi") this.array1 = ko.observableArray(["hi", "there"]); this.array2 = ko.observableArray([ko.observable("hi"), ko.observable("there")]); this.array3 = ko.observableArray([{ data: "hi" }, { data: "there" }]); }; ko.applyBindings(new ViewModel()); </code></pre> <p>HTML:</p> <pre><code>&lt;div class='liveExample'&gt; &lt;p&gt;&lt;input data-bind='value: value' /&gt;&lt;/p&gt; &lt;div data-bind="foreach: array1"&gt; &lt;p&gt;&lt;input data-bind='value: $data' /&gt;&lt;/p&gt; &lt;/div&gt; &lt;div data-bind="foreach: array2"&gt; &lt;p&gt;&lt;input data-bind='value: $data' /&gt;&lt;/p&gt; &lt;/div&gt; &lt;div data-bind="foreach: array3"&gt; &lt;p&gt;&lt;input data-bind='value: data' /&gt;&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;pre data-bind="text: ko.toJSON($data)"&gt;&lt;/pre&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload