简体   繁体   中英

How to pass a parameter to a computed function in Knockout?

This is actually a continuation of this question that I asked earlier.

I needed to pass an element to a function to check if one of its children had a checked checkbox, and I got that working (described in the question above), but the answer given does not make any allowances for the checkbox to be observed. How would I go about detecting a change in a child element when my Knockout Binding is on a parent?

I cannot pass a parameter to ko.computed as I am told:

Cannot write a value to a ko.computed unless you specify a 'write' option

Any ideas?

Please check the writable computed article:

var _innerValue = 0;
var computedValue = ko.computed({
        read: function() {
            // you can put here some additional logic
            return _innerValue;
        },
        write: function(newValue) {
            // you can put here some additional logic
            _innerValue = newValue;
        }
    });

If you don't need any specific logic, you can use simple ko.observable .

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