Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing value of ko.observable

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?

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?

like image 232
Bartosz Avatar asked Jan 04 '13 15:01

Bartosz


People also ask

How do you set a value in Ko observable?

To write a new value to the observable, call the observable and pass the new value as a parameter. For example, calling myViewModel. personName('Mary') will change the name value to 'Mary' . To write values to multiple observable properties on a model object, you can use chaining syntax.

How do you declare a viewModel property as observable?

Syntax. You just need to declare ViewModel property with function ko. observable() to make it Observable.

What does Ko unwrap do?

unwrap method is used to read it), the current viewModel and bindingContext. Whenever the passed value changes the binding will print updated information to console. This binding cannot be used with virtual elements (in html comments), only on real elements, since ko.


2 Answers

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

like image 144
sellmeadog Avatar answered Sep 24 '22 23:09

sellmeadog


You can change value of observable like this:

feature.totalLength(10) 
like image 29
bjornd Avatar answered Sep 23 '22 23:09

bjornd