How to get the component's viewmodel from component's node in KnockoutJS

1.9k Views Asked by At

I have a component named 'my-component':

ko.components.register('my-component', { 

    viewModel: function() {      
        return { title: 'title' };     
    },

    template: '<div>x</div>'

});

I'm using this component in the view like that:

<my-component params=""></my-component>

Is there any way to get the component's view-model related to this HTML node? ko.dataFor doesn't work:

ko.dataFor($('my-component').get(0)) // it doesn't return component's view model

Any thoughts?

1

There are 1 best solutions below

5
On BEST ANSWER

Try this:

ko.dataFor($('my-component').get(0).firstChild);