Knockoutjs computed passing parameters

前端 未结 4 2045
终归单人心
终归单人心 2021-01-31 15:01

I am wondering if it is possible with knockoutjs to pass arguments when binding.

I am binding a list of checkboxes and would like to bind to a single computed observabl

4条回答
  •  礼貌的吻别
    2021-01-31 15:34

    Without knowing the specifics, it seems like what you should be doing is defining a ko.observableArray or computed array value

    HTML:

    myprop: 
    

    JS:

      $(function() {
        function Model() {
            this.self = this
            self.myprop = ko.observable(14)
            self.bits = [1, 2, 4, 8, 16, 32, 64, 128]
            self.selections = ko.computed(function() {
                return self.bits.map(function(bit) {
                    console.log(myprop() & bit)
                    return {
                        value: bit,
                        selected: (myprop() & bit) == bit
                    }
                })
            })
        }
    
        ko.applyBindings(new Model())
    })
    

    and not passing values from the markup to define the model state

提交回复
热议问题