在Knockout.js中未定义$ parentContext.$ index [英] $parentContext.$index is not defined in Knockout.js

查看:138
本文介绍了在Knockout.js中未定义$ parentContext.$ index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历数据并尝试创建运行时html,我无法从集合中获取索引,我正在使用淘汰赛2.1.0.js.我在做错什么吗?

I am looping through the data and trying to create runtime html I am unable to fetch the index from the collection, I m using knockout-2.1.0.js. Am I doing something wrong?

<ul data-bind="foreach: gridOptions.columns">    
    <li>     
        <label data-role="none" data-bind="attr:{for: 'chk' + $parentContext.$index }">     
        </label>
        <input type="checkbox" data-bind="attr:{ id: 'chk' + $parentContext.$index }, checked: checked}" /><span data-bind="text:header"></span>
    </li>    
</ul>

Viewmodel是gridOptions:

Viewmodel is gridOptions:

{
    columns: [{
        header: 'Catalog Number',
        dataMember: 'productName',
        checked: ko.observable(true)
    }, {
        header: 'Description',
        dataMember: 'shortDesc',
        checked: ko.observable(true)
    }]
},

请坚持这样做,请为我提供帮助.我已经尝试过用普通的$index$index()进行所有可能的操作,但无法完成这项工作.

Please help me on this as I am stuck with this. I have tried with plain $index or $index() every possible thing but couldn't make this work.

推荐答案

data-bind="attr: { 'for': 'chk' + $index() }"
删除}:
data-bind="attr: { 'id': 'chk' + $index() }, checked: checked"

data-bind="attr: { 'for': 'chk' + $index() }"
Remove }:
data-bind="attr: { 'id': 'chk' + $index() }, checked: checked"

工作示例: http://jsfiddle.net/myS95/3/.

这篇关于在Knockout.js中未定义$ parentContext.$ index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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