Javascript 改变 ko.observable 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14159574/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:51:38  来源:igfitidea点击:

changing value of ko.observable

javascriptknockout.js

提问by Bartosz

I have a ko.observable property of an object called "totalLength". While using application I would like to physically amend new value to this property. How can I do that?

我有一个名为“totalLength”的对象的 ko.observable 属性。在使用应用程序时,我想物理修改此属性的新值。我怎样才能做到这一点?

I can preview the value of the demanded property by displaying:

我可以通过显示来预览所需属性的值:

alert(feature.totalLength());

so I know that it is the one. But when I assign a new value to it:

所以我知道它是一个。但是当我为它分配一个新值时:

feature.totalLength() = 10;

I get an error:

我收到一个错误:

ReferenceError: invalid assignment left-hand side

参考错误:左侧无效分配

Why?

为什么?

回答by sellmeadog

ko.observableis a function so you need to set the value like this feature.totalLength(10).

ko.observable是一个函数,因此您需要像这样设置值feature.totalLength(10)

回答by bjornd

You can change value of observable like this:

您可以像这样更改 observable 的值:

feature.totalLength(10)