knockout.js 关于用于嵌套的数组无关系的foreach多层嵌套报undefined的错误

问题描述:

我要实现ko的foreach多层嵌套,用于foreach的两个数组没有关系,第二层foreach绑定的数组报错:
Message: Unable to process binding "foreach: function (){return p2 }"
Message: p2 is not defined

代码:

js:
viewModel = {

p1 : ko.observableArray(
[
{ firstName: 'Bert', lastName: 'Bertington' },
{ firstName: 'Charles', lastName: 'Charlesforth' },
{ firstName: 'Denise', lastName: 'Dentiste' }
]
),
p2 : ko.observableArray(
[
{ firstName: 'Bert', lastName: 'Bertington' },
{ firstName: 'Charles', lastName: 'Charlesforth' },
{ firstName: 'Denise', lastName: 'Dentiste' }
]
),
}

html:

<div data-bind="foreach:p1">
    <div>我是p1</div>
    <div data-bind="foreach:p2">
        <li>我是p2</li>
    </div>
</div>
报错:

knockout-3.2.0.debug.js:2890 Uncaught ReferenceError: Unable to process binding "foreach: function (){return p1 }"
Message: Unable to process binding "foreach: function (){return p2 }"
Message: p2 is not defined
    at foreach (eval at createBindingsStringEvaluator (knockout-3.2.0.debug.js:2503), <anonymous>:3:60)
    at knockout-3.2.0.debug.js:3739
    at Object.init (knockout-3.2.0.debug.js:4859)
    at init (knockout-3.2.0.debug.js:3764)
    at knockout-3.2.0.debug.js:2867
    at Object.ignore (knockout-3.2.0.debug.js:1194)
    at knockout-3.2.0.debug.js:2866
    at Object.arrayForEach (knockout-3.2.0.debug.js:121)
    at applyBindingsToNodeInternal (knockout-3.2.0.debug.js:2852)
    at applyBindingsToNodeAndDescendantsInternal (knockout-3.2.0.debug.js:2732)

请各位大牛帮忙看看。
谢谢。



Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐