How to pass value from one view model to another viewmodel in knockout js?

前端 未结 3 957
不思量自难忘°
不思量自难忘° 2021-01-22 00:51

i have two view models and i want to pass value from one view model to another viewmodel. i have two viewmodels and two div i want to display another div on click of button whic

3条回答
  •  余生分开走
    2021-01-22 01:24

    For this you can follow these

    Viewmodels

    function vm1(parent){
        var self = this
        self.parent = ko.observable(parent)
        .
        .
        .
    
        self.myfunction(data){
            self.parent().pageParameters(data)
        }
    }
    function vm2(parent){
        var self = this
        self.parent = ko.observable(parent)
        .
        .
        .
    
        self.myotherfunction(){
            var othervmdata = self.parent().pageParameters()
            .
            .
        }
    }
    
    function vm(){
        var self = this
    
        self.vm1 = ko.observable();
        self.vm2 = ko.observable();
    
        self.pageParameters = ko.observable
    
        self.loadData = function(){
            self.vm1(new vm1(this))
            self.vm2(new vm1(this))
        }
    }
    

    View

    . . .
    . . .

    This way when you bind myfunction to click binding parent model will have the data you want to pass then on the other view model you can access parent property pageParameters.

    For more details see my post.

提交回复
热议问题