Knockoutjs模板foreach,特殊的第一项 [英] Knockoutjs template foreach, special first item

查看:162
本文介绍了Knockoutjs模板foreach,特殊的第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在想办法解决这个问题.我有一个foreach,吐出模板,并且我希望第一个元素具有特殊的属性.到目前为止,我发现解决方案还没有奏效.

So i have been busting my brain figuring this out. I have a foreach, spitting out templates, and i want the first element to have a special attribute. So far solutions i have found havent worked.

这是foreach:

<h3 class="question">Geografi</h3>   
        <p class="answer" data-bind="template: { name: 'geographyTmpl', foreach: geographyList,  templateOptions: { selections: selectedGeographies } }"></p>

这是模板:

<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>

我想在第一个元素上添加:"validate =" required:true,minlength:2.

And i want to add: "validate="required:true, minlength:2" to the first element.

我需要做什么?

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

If it helps, its for jQuery validation.

推荐答案

检查我的答案是否还有关于KnockoutJS foreach

check my answer for another question about the first element in KnockoutJS foreach

在foreach敲除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>

这篇关于Knockoutjs模板foreach,特殊的第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆