Knockout.js using `value:` binding in a `foreach` over a list of strings – does not update

Knockout.js using `value:` binding in a `foreach` over a list of strings – does not update

Asked on December 24, 2018 in knockout js.
Add Comment


  • 2 Answer(s)

        This answer is very useful in this case :

        Form a new binding   by  using  this  value: $parent.list[$index()]  .

        Try this code :

    <ul data-bind='foreach: list'>
        <li>
            <input data-bind='value: $parent.list[$index()]' />
        </li>
    </ul>
    

        Use this custom binding to improve on this.

        Try this  GitHub issue #708  for Knockout.js.

        The Knockout 3.0 updates :

        The Knockout is now giving the   $rawData :

    <ul data-bind='foreach: list'>
        <li><input data-bind='value: $rawData'/></li>
    </ul>
    

        Make a two-way binding as expected.

        From the Binding Context  documentation:

    $rawData

        This is the raw view model value in the present context . This is equal  as $data , but if the view model given to Knockout is covered in an observable, $data will be the unwrapped view model, and $rawData will be the observable itself.

    Answered on December 24, 2018.
    Add Comment

        All  data object used in the default knockout bindings will always be not covered . So you are essentially binding to the value of the items in the list, not the observable as per the user wish.

        Observables should be properties of an object , not a replacement of the object itself . Set the observables as a property of some object so this will not happen again .

    ​var vm = {
        list: [
            { value: ko.observable('123') },
            { value: ko.observable('456') }
        ]
    };
    
    <ul data-bind='foreach: list'>
        <li><input data-bind='value: value'/></li>
    </ul>
    <ul data-bind='foreach: list'>
        <li><span data-bind='text: value'></span></li>
    </ul>
    

     

    Answered on December 24, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.