knockout template binding

徘徊边缘 提交于 2019-12-04 07:30:34

You can use containerless control flow syntax, databinding using comment tags. No need for a template. more info

<ul>     
    <li><button data-bind=click: doSomething">Click me</button></li>
    <!-- ko foreach: someElemets-->         
    <li>
        <span data-bind="text: someText"></span>
    </li>    
    <!-- /ko -->
</ul> 

The following will do it:

<!-- ko template: { name: 'template-name', data: vm } --> <!-- /ko -->
Mark Robinson

I'm not aware of an easy way to access the index when inside a template. You could use template options as described at How to use foreach with a special first element?

Your code would be something like:

<ul data-bind="template: { name: 'someTemplate', foreach: someElemets, templateOptions: { first: someElemets()[0]} }">
</ul>

<script id="someTemplate" type="text/html">
    <li>
    {{if $item.first === $data}}
      <button data-bind="click: doSomething">Click me</button>
    {{/if}}
    <span data-bind="text: someText">
    </li>
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!