user3757174
user3757174

Reputation: 523

Knockout.js not updating value of observable

I'm having trouble getting Knockout to update the value of my observable.

Model:

function AppViewModel() {
   var self = this;
   self.observable = ko.observable();
   self.test = function() {
       self.observable("test")
   }
};
var model = new AppViewModel();
ko.applyBindings(model);

View:

<p>Value of observable: <input data-bind="value: $root.observable()" /></p>
<p>The value is: <span data-bind="text: $root.observable()"></span></p>

When I type text into the input field, it should update the value of my observable to what I've typed, correct? But the 2nd paragraph doesn't update its text accordingly.

Thanks!

Upvotes: 0

Views: 1587

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134841

Remove the parentheses in your bindings. You want to bind to the observable itself, not the value the observable is holding.

<p>Value of observable: <input data-bind="value: $root.observable" /></p>
<p>The value is: <span data-bind="text: $root.observable"></span></p>

Upvotes: 4

Related Questions