javascript 在 Knockout 中不更新 Observable 的设置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19391415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting value of Observable not updating in Knockout
提问by PW Kad
(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)
(每天都有大量问题链接回为什么我不能设置我的 observable 的值,而不是有这么多不同的答案说同样的事情,我想创建一个问题供大家参考)
Setting value of Knockout Observable / Observable Array doesn't update
Knockout Observable / Observable Array 的设置值不更新
Setting the value of my observable observableArray isn't updating!
设置我的 observable observableArray 的值没有更新!
Adding an item to an Observable Array
向 Observable 数组添加项目
Why can't I add an item into my Knockout observable array?
为什么我不能将一个项目添加到我的 Knockout 可观察数组中?
回答by PW Kad
Setting value of Knockout Observable / Observable Array doesn't update
Knockout Observable / Observable Array 的设置值不更新
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
NoteSee 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
向 Observable 数组添加项目
You need to push the item into the observableArray, notthe 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');