Updating an observableArray does not update UI

前端 未结 1 1798
迷失自我
迷失自我 2021-01-05 00:26

I am using the containerless flow control in ko 2.0. When I update an item in my observableArray it is not updating the UI. I am updating the array like this:



        
相关标签:
1条回答
  • 2021-01-05 01:26

    Here is a a fiddle that demonstrates how to replace an item in an observableArray and have its changes notify the UI.

    http://jsfiddle.net/johnpapa/ckMJE/

    The key here is the replace function on the observableArray. You could also use splice.

    ... Notice the use of "replace" below ...

    var ViewModel = function() {
        this.self = this;
        self.index = ko.observable(0); // default
        self.newColor = ko.observable("purple"); // default
        self.colors = ko.observableArray([{
            color: 'red'},
        {
            color: 'blue'},
        {
            color: 'yellow'}]);
        self.replaceIt = function() {
            self.colors.replace(self.colors()[self.index()], {
                color: self.newColor()
            });
        };
    };
    ko.applyBindings(new ViewModel());
    
    0 讨论(0)
提交回复
热议问题