看一下这种情况:
ko.components.register('hello', {
viewModel: function() { },
template: "<h1>hello wrold</h1>"
});
如果我使用<hello></hello>
生成的html结果将是:
<hello><h1>hello world</h1></hello>
但是,如果我想要这样:
<hello class="hello"><h1>hello world</h1></hello>
然后,如何获得对组件中自定义元素标签的引用?
custom元素包含该组件,但不认为它是其中的一部分。就像在使用的外层标签foreach
,template
或with
结合。如果要设置该标签的样式,则必须添加绑定以对其进行样式设置。该组件将填充其内容。
<hello data-bind="css: 'hello'"></hello>
但是,如果您绝对想访问父元素,我想有可能,但我不建议这样做。该组件应仅与自身有关,而与包含它的容器无关。如果元素具有任何也具有绑定的子节点,则可能(并且将)引起问题。
对视图模型使用工厂功能。它将有权访问组件的信息(当前仅包含contains元素element
)
ko.components.register('hello', {
viewModel: {
createViewModel: function (params, componentInfo) {
var element = componentInfo.element;
ko.applyBindingsToNode(element, { css: 'hello' });
return {};
}
},
template: "<h1>hello world</h1>"
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句