Reputation: 3014
I want to explicitly change a knockout observable. I tried doing something like this but what happened was the integer was written to the variable and the observable was over written:
self.test = ko.observable();
var array = [1,3,4];
self.test = array.length; //self.test is no longer an observable, is now an int with value of 3
I tried doing something like this too:
self.numOfAsstsInUpdateGrid() = self.dynamicData.length;
But I get an "Invalid left-hand side in assignment" error
Upvotes: 0
Views: 103
Reputation: 9
self.test(array.length);
This will preserve test as an observable and set it equal to array.length. Whenever you're setting the value of an observable, you pass the desired value in the parentheses.
Upvotes: 1
Reputation: 181
If you plan on using an array I'd recommend using ko.observableArray. Which has some nice helper functions like remove. Changing the value of observable can be done by passing a value into the observable function. Like so
self.test = ko.observable();
self.test([1,3,4]);
Calling the function will return the value.
var length = self.test().length;
Upvotes: 0