在 Knockout 中不更新 Observable 的设置值 [英] Setting value of Observable not updating in Knockout

查看:24
本文介绍了在 Knockout 中不更新 Observable 的设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(每天都有大量问题链接回为什么我不能设置我的 observable 的值,而不是有这么多不同的答案说同样的事情我想创建一个问题来参考大家)

(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 observableArray 的值没有更新!

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

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

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

推荐答案

Knockout Observable/Observable Array 的设置值不更新

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

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 -

例如.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

例如.2 (observableArray) -

Ex. 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

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

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?

需要将item压入observableArray中,不是observableArray的底层值-

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

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

创建一个模型以在您的视图模型中使用/共享

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

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.

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

可以像 -

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');

这篇关于在 Knockout 中不更新 Observable 的设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆