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?
A ViewModel can be any type of JavaScript variable. In Example 1-3, let's start with a simple JavaScript structure that contains a single property called name .
This is the raw view model value in the current context. Usually this will be the same as $data , but if the view model provided to Knockout is wrapped in an observable, $data will be the unwrapped view model, and $rawData will be the observable itself.
Shorthand syntax: If you just supply a string value, KO will interpret this as the ID of a template to render. The data it supplies to the template will be your current model object.
Try this:
ko.dataFor($('my-component').get(0).firstChild);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With