在Knockout中设置Observable的值不更新

时间:2022-12-03 10:28:23

(There are a ton of questions every day that link back to why can't I set the value of my observable, instead of having so many different answers saying the same thing I wanted to create a question to refer back to for everyone)

(每天都有大量的问题链接回原因为什么我不能设置我的观察值,而不是有这么多不同的答案说同样的事情,我想创建一个问题,以回顾每个人)

Setting value of Knockout Observable / Observable Array doesn't update

Setting the value of my observable observableArray isn't updating!

设置我的observable observableArray的值不会更新!

Adding an item to an Observable Array

Why can't I add an item into my Knockout observable array?

为什么我不能将项添加到我的Knockout可观察数组中?

1 个解决方案

#1


7  

Setting value of Knockout Observable / Observable Array doesn't update

You need to use the setter function to update the value of your observable / observableArray -

您需要使用setter函数来更新observable / observableArray的值 -

Ex. 1 (observable) -

防爆。 1(可观察) -

var name = 'John';
var myValue = ko.observable();
myValue(name); // Set myValue equal to John, update any subscribers

var newObservable = ko.observable('Bill');
myValue(newObservable()); // Set myValue equal to the value of newObservable, which is Bill

Ex. 2 (observableArray) -

防爆。 2(observableArray) -

var names = ['John', 'William', 'Dave'];
var myArray = ko.observableArray();
myArray(names); // Set myArray equal to the array of names John, update any subscribers

var newArray = ko.observableArray(['Sanford']);
myArray(newArray()); // Makes a clone of the array

Note See this Question to understand why this probably not what you are trying to do - What is the best way of cloning/copying an observablearray in knockoutJS?

注意请参阅此问题以了解为什么这可能不是您要做的 - 在knockoutJS中克隆/复制observablearray的最佳方法是什么?

Adding an item to an Observable Array

You need to push the item into the observableArray, not the underlying value of the observableArray -

您需要将项目推入observableArray,而不是observableArray的基础值 -

var name = 'John';
var myValue = ko.observable(name);
var myArray = ko.observableArray();
myValue.push(myValue()); // Add myValue to my observableArray**

Creating a model to use/share in your view model

You can create a re-usable model to use in your view models. This is similar to using a class in C# to create objects that have common properties.

您可以创建可在模型中使用的可重用模型。这类似于在C#中使用类来创建具有公共属性的对象。

function objectModel(item) {
    var self = this;
    self.Name = ko.observable(item.name);
    self.Description = ko.observable(item.description);
}

Which can be created like -

哪个可以像 -

var object = {
    name: 'John',
    description: 'a person'
}

var john = new objectModel(object);

Which could also be done by parameters instead of just objects -

这也可以通过参数而不仅仅是对象来完成 -

function objectModel(name, description) {
    var self = this;
    self.Name = ko.observable(name);
    self.Description = ko.observable(description);
}

var john = new objectModel('John', 'a person');

#1


7  

Setting value of Knockout Observable / Observable Array doesn't update

You need to use the setter function to update the value of your observable / observableArray -

您需要使用setter函数来更新observable / observableArray的值 -

Ex. 1 (observable) -

防爆。 1(可观察) -

var name = 'John';
var myValue = ko.observable();
myValue(name); // Set myValue equal to John, update any subscribers

var newObservable = ko.observable('Bill');
myValue(newObservable()); // Set myValue equal to the value of newObservable, which is Bill

Ex. 2 (observableArray) -

防爆。 2(observableArray) -

var names = ['John', 'William', 'Dave'];
var myArray = ko.observableArray();
myArray(names); // Set myArray equal to the array of names John, update any subscribers

var newArray = ko.observableArray(['Sanford']);
myArray(newArray()); // Makes a clone of the array

Note See this Question to understand why this probably not what you are trying to do - What is the best way of cloning/copying an observablearray in knockoutJS?

注意请参阅此问题以了解为什么这可能不是您要做的 - 在knockoutJS中克隆/复制observablearray的最佳方法是什么?

Adding an item to an Observable Array

You need to push the item into the observableArray, not the underlying value of the observableArray -

您需要将项目推入observableArray,而不是observableArray的基础值 -

var name = 'John';
var myValue = ko.observable(name);
var myArray = ko.observableArray();
myValue.push(myValue()); // Add myValue to my observableArray**

Creating a model to use/share in your view model

You can create a re-usable model to use in your view models. This is similar to using a class in C# to create objects that have common properties.

您可以创建可在模型中使用的可重用模型。这类似于在C#中使用类来创建具有公共属性的对象。

function objectModel(item) {
    var self = this;
    self.Name = ko.observable(item.name);
    self.Description = ko.observable(item.description);
}

Which can be created like -

哪个可以像 -

var object = {
    name: 'John',
    description: 'a person'
}

var john = new objectModel(object);

Which could also be done by parameters instead of just objects -

这也可以通过参数而不仅仅是对象来完成 -

function objectModel(name, description) {
    var self = this;
    self.Name = ko.observable(name);
    self.Description = ko.observable(description);
}

var john = new objectModel('John', 'a person');