knockout.js:更新绑定? [英] knockout.js: update bindings?
问题描述
当我在ko.applyBindings()之后将任何新元素注入到DOM中时被召唤,然后淘汰赛将无法识别这些新元素。
我可以理解为什么会发生这种情况 - 他们只是没有被淘汰索引。
when I inject any new elements into the DOM after ko.applyBindings(); was called, then knockout won't recognize these new elements. I can understand why this is happening - they are just not indexed by knockout.
所以,起初我以为只要拨打ko就可以解决这个问题。在添加我的新元素之后再次applyBindings()然后我意识到,对于你做的每个ko.applyBindings()调用,相应的事件会多次被触发。因此,在应用五次后,点击:绑定将被触发五次,因此这不是一个理想的解决方案;)
So, at first I thought this would be solved by just calling ko.applyBindings() again, after adding my new elements, BUT then I realized that for every ko.applyBindings() call you make, the according events get fired multiple times. So after applying five times, a click: binding will be fired five times, so this is not a desireable solution ;)
是否有类似ko.updateBindings()或别的什么,告诉敲门,好......更新元素绑定?
Is there anything like ko.updateBindings() or something else, to tell knockout to, well... update the element bindings?
问候,
Chris
greetings, Chris
推荐答案
每次调用 ko.applyBindings
时,都会检查整个DOM是否存在绑定。因此,如果您多次执行此操作,您将获得每个元素的多个绑定。如果您只想绑定一个新的DOM元素,可以将此元素作为参数传递给 applyBindings
函数:
Each time you invoke ko.applyBindings
the entire DOM is inspected for bindings. As a result you will get multiple bindings for each element if you do this more than once. If you just want to bind a new DOM element you can pass this element as a parameter to the applyBindings
function:
ko.applyBindings(viewModelA, document.getElementById("newElement"));
请参阅此相关问题:
< a href =https://stackoverflow.com/questions/7342814/knockoutjs-ko-applybindings-to-partial-view>你能调用ko.applyBindings绑定部分视图吗?
这篇关于knockout.js:更新绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!