我有knockout.js工作这样的一段代码:匿名模板knockoutJS
<div>
... some other markup here
<div class="topicDetail" data-bind='template: { name: "topicTemplate", data: activeTopic}'> </div>
</div>
<script type="text/html" id="topicTemplate">
<ul class="querylist" data-bind='template: {name: "queryTemplate", foreach: queries}'></ul>
</script>
<script type="text/html" id ="queryTemplate">
<li class="query" data-bind="css: { active: selected()}, queryType: type">
<span class="querylink" data-bind="click: select">{{= text}}</span>
<span data-bind="withdocs: positiveExamples"></span>
<span data-bind='person: searcher'> </span>
<span data-bind='time: time'></span>
</li>
</script>
withdocs
,person
和time
是我的自定义绑定。我想我应该更简洁地重写它使用匿名的模板是这样的:
<div class="topicDetail" data-bind="with: activeTopic">
<ul class="querylist" data-bind="foreach: queries">
<li class="query">
<span class="querylink" data-bind="click: select">{{= text}}</span>
<span data-bind="withdocs: positiveExamples"></span>
<span data-bind='person: searcher'> </span>
<span data-bind='time: time'></span>
</li>
</ul>
</div>
但这种失败,出现错误:
Uncaught Error: Unable to parse binding attribute.
Message: ReferenceError: queries is not defined;
Attribute value: foreach: queries
在淘汰赛1.2.1.debug.js的1226线。这是指UL
数据绑定。
我创建了一个jsfiddle,它抽象出这个问题,但小提琴的作品。还有什么我应该关注的?
这听起来像你有一个情况,至少最初是在'activeTopic'中有什么东西,但没有'queries'数组。 'activeTopic'如何初始化?它会有没有'查询'数组? –
是的,匿名模板在KO 1.3中工作。你正在使用1.2。 –