gpt4 book ai didi

javascript - Knockout ObservableArray 未在 UI 中更新

转载 作者:行者123 更新时间:2023-12-02 16:21:01 24 4
gpt4 key购买 nike

我正在尝试更新绑定(bind)到 ObservaleArray 的数组值。但是它没有得到更新。

function AppViewModel() {
var self = this;
self.Title=ko.observable('Sample');
self.people = ko.observableArray([
new Person('Ajay'),
new Person('Kumar')
]);

self.updateAns=function(){
self.people()[0].Answered=false;
};

self.updateName=function(){
self.people()[0].Name('John');
};
self.updateTitle=function(){
self.Title('New Title');
};

}

var Person=function(name){
this.Name=name;
this.Answer=[{Id: 1, Answered:true},{Id:2, Answered:true}]
}

ko.applyBindings(new AppViewModel());

Full Demo

如何保持数组值同步?

最佳答案

observable 数组只会监听数组本身的变化,如果你想让 Person 上的成员更新,你还需要它们是 observables

关于javascript - Knockout ObservableArray 未在 UI 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29118457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com