Knockoutjs模板foreach,特别第一项

Fre*_*k T 13 jquery-validate knockout.js

所以我一直在破坏我的大脑.我有一个foreach,吐出模板,我希望第一个元素具有特殊属性.到目前为止,我发现的解决方案还没有奏效.

这是foreach:

<h3 class="question">Geografi</h3>   
        <p class="answer" data-bind="template: { name: 'geographyTmpl', foreach: geographyList,  templateOptions: { selections: selectedGeographies } }"></p>
Run Code Online (Sandbox Code Playgroud)

这是模板:

<script id="geographyTmpl" type="text/html">
<input class="geoCheckList" validate="required:true, minlength:2" name="geoCheckList[]" type="checkbox" data-bind='value: $data, attr: { id: "Geo"+$index()},checked: $root.selectedGeographies' />
<label data-bind="text: $data, attr: { 'for': 'Geo'+$index()}"></label>
Run Code Online (Sandbox Code Playgroud)

我想补充:"validate ="required:true,minlength:2"到第一个元素.

我需要做什么?

如果它有帮助,它用于jQuery验证.

ebr*_*lil 31

检查我关于KnockoutJS中第一个元素的另一个问题的答案 foreach

跳过foreach knockout js数组中的项目?

<div data-bind="text: ItemsArray[0].someProperty"></div>
<div data-bind="foreach: ItemsArray">
<!-- ko if: $index() == 0 -->
     <div data-bind="text: someProperty"></div>
 <!-- /ko -->
<!-- ko if: $index() != 0 -->
    <div data-bind="text: anotherDifferentProperty"></div>    
 <!-- /ko -->
</div>
Run Code Online (Sandbox Code Playgroud)