what is the difference between <!--ko--> binding and visible binding in knockout?

北城余情 提交于 2019-12-14 01:13:12

问题


ko <!--ko--> binding and data-bind="visible:sometext" binding

Both binding did the same operation and which one is efficient for use and which one is best


回答1:


<!-- ko [binding] --> allows you to bind to an virtual element.

data-bind="[binding]" is a regular binding and can only be applied to actual elements.

The difference is that the first can be used without having to create an element:

<ul>
    <li class="heading">My heading</li>
    <!-- ko foreach: items -->
        <li data-bind="text: $data"></li>
    <!-- /ko -->
</ul>

Do note that not every binding handler can be applied to an virtual element:

Custom bindings can work with virtual elements too, but to enable this, you must explicitly tell Knockout that your binding understands virtual elements, by using the ko.virtualElements.allowedBindings API.




回答2:


Virtual elements :

  • Can't be use to change the style of the element, because there is no bound dom element.

e.g. :

<!-- ko visible: prop--><!-- /ko -->
  • Are useful to control flow when you can't add an extra element :

e.g. :

<table>
    <tbody>
        <tr><td></td></tr>
        <!-- ko if: cond -->
            <tr><td>Optionnal line </td></tr>
         <!-- /ko  -->
    </tbody>
</table>

Actual elements data-bind :

  • Can be used to control flow (if, foreach, with) and to modify the element attributes (style, id, class, etc).


来源:https://stackoverflow.com/questions/20971150/what-is-the-difference-between-ko-binding-and-visible-binding-in-knockout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!