How to clear/remove observable bindings in Knockout.js?

后端 未结 9 1850
忘了有多久
忘了有多久 2020-11-29 16:40

I\'m building functionality onto a webpage which the user can perform multiple times. Through the user\'s action, an object/model is created and applied to HTML using ko.app

相关标签:
9条回答
  • 2020-11-29 17:34

    Instead of using KO's internal functions and dealing with JQuery's blanket event handler removal, a much better idea is using with or template bindings. When you do this, ko re-creates that part of DOM and so it automatically gets cleaned. This is also recommended way, see here: https://stackoverflow.com/a/15069509/207661.

    0 讨论(0)
  • 2020-11-29 17:38

    I have to call ko.applyBinding each time search button click, and filtered data is return from server, and in this case following work for me without using ko.cleanNode.

    I experienced, if we replace foreach with template then it should work fine in case of collections/observableArray.

    You may find this scenario useful.

    <ul data-bind="template: { name: 'template', foreach: Events }"></ul>
    
    <script id="template" type="text/html">
        <li><span data-bind="text: Name"></span></li>
    </script>
    
    0 讨论(0)
  • 2020-11-29 17:41

    Have you tried calling knockout's clean node method on your DOM element to dispose of the in memory bound objects?

    var element = $('#elementId')[0]; 
    ko.cleanNode(element);
    

    Then applying the knockout bindings again on just that element with your new view models would update your view binding.

    0 讨论(0)
提交回复
热议问题