简体   繁体   中英

Knockout computed observable not working

I am using a Knockout computed observable to multiply two other observables. Unfortunately, the computed observable does not seem to be outputting any value.

var boyle1 = {
volume1_text: ko.observable(parseInt(2)),
volume1_select: ko.observable(parseInt(2))
};
boyle1.volume = ko.computed(function () {
    return this.volume1_text() * this.volume1_select();
}, boyle1);
ko.applyBindings(boyle1);

Both other observables work perfectly, and are easily binded to elements on the page. What am I doing wrong?

It has something to do with your html binding to the computed. As you indicated it was in fact, a typo in the markup.

Markup

<input data-bind="value: volume1_text" />
<input data-bind="value: volume1_select" />
<br /><br />    
<span data-bind="text: volume"></span>

Model

var boyle1 = {
   volume1_text: ko.observable(parseInt(2)),
   volume1_select: ko.observable(parseInt(2))
};

boyle1.volume = ko.computed(function () {
    return this.volume1_text() * this.volume1_select();
}, boyle1);

ko.applyBindings(boyle1);

Here is a working example of your model and problem

http://jsfiddle.net/YBr2m/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM