[英]Knockout update observableArray item does not reflect in UI?
The problem when my viewModel has observableArray (self.images) and I want when one item property (mm) is changed to be reflected on UI 当我的viewModel具有observableArray(self.images)并且我希望将一项属性(mm)更改为反映在UI上时的问题
in the real code the images is loaded from server and mapped to object DOcImage 在真实代码中,图像是从服务器加载并映射到对象DOcImage
here is the code note that i have been trying few things based on other questions answers but no luck yet 这是代码说明,根据其他问题我一直在尝试一些尝试,但还没有运气
here is fiddle http://jsfiddle.net/mosta/jt6bbeq4/21/ when click "change" it should display first item = 2, according to line in upate function 这是小提琴http://jsfiddle.net/mosta/jt6bbeq4/21/当单击“更改”时,应根据更新功能中的行显示第一项= 2
var DocImage = function () {
var self = this;
self.mm = ko.observable('1');
self.nn = ko.observable('1');
};
var viewModel = function () {
var self = this;
self.images = ko.observableArray([DocImage]);
self.update = function () {
self.images()[0].mm = '2';
}
};
var vm = new viewModel();
vm.images = [{ mm: ko.observable('1') ,nn: ko.observable('2') }, {mm:ko.observable('3'), nn:ko.observable('4')}];
ko.applyBindings(vm);
Appreciate your help , thanks 感谢您的帮助,谢谢
When you set 设定时
m.images = [{ mm: ko.observable('1') ...
you actually re-write your observableArray with simple array. 您实际上使用简单数组重写了observableArray。 To update the value inside observable array you need to use another syntax (See documentation )
要更新可观察数组中的值,您需要使用其他语法(请参阅文档 )
m.images([{ mm: ko.observable('1') ...);
The same is true for simple observables like mm
对于
mm
简单可观测对象也是如此
self.images()[0].mm('2');
Here you go: 干得好:
var viewModel = function () {
var self = this;
self.images = ko.observableArray([DocImage]);
self.update = function () {
self.images[0].mm('2');
}
};
var vm = new viewModel();
vm.images = [{ mm: ko.observable('1') ,nn: ko.observable('2') }, {mm:ko.observable('3'), nn:ko.observable('4')}];
ko.applyBindings(vm);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.