How to force a view refresh without having it trigger automatically from an observable?

前端 未结 3 982
鱼传尺愫
鱼传尺愫 2020-11-29 18:12

Note: this is mostly for debugging and understanding KnockoutJS.

Is there a way to explicitly request Knockout to refresh the view from (already bou

相关标签:
3条回答
  • 2020-11-29 18:21

    In some circumstances it might be useful to simply remove the bindings and then re-apply:

    ko.cleanNode(document.getElementById(element_id))
    ko.applyBindings(viewModel, document.getElementById(element_id))
    
    0 讨论(0)
  • 2020-11-29 18:23

    You can't call something on the entire viewModel, but on an individual observable you can call myObservable.valueHasMutated() to notify subscribers that they should re-evaluate. This is generally not necessary in KO, as you mentioned.

    0 讨论(0)
  • 2020-11-29 18:33

    I have created a JSFiddle with my bindHTML knockout binding handler here: https://jsfiddle.net/glaivier/9859uq8t/

    First, save the binding handler into its own (or a common) file and include after Knockout.

    If you use this switch your bindings to this:

    <div data-bind="bindHTML: htmlValue"></div>
    
    OR
    
    <!-- ko bindHTML: htmlValue --><!-- /ko -->
    
    0 讨论(0)
提交回复
热议问题