Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我已经在这个小提琴中隔离了我的问题
http://jsfiddle.net/xYphz/
它使用根模型而不是父模型
编辑:一个明显的解决方案是使用模板,但必须有一种更简单的方法来获取正确的上下文?
http://jsfiddle.net/eHrBR/
通常你会做这样的事情:
<!-- ko with: subModel --> <div data-bind="foreach: items"> <button data-bind="click: $parent.clickMe">Click me</button> </div> <!-- /ko -->
这可确保父上下文是您的subModel. 当然,您可以将with绑定放在元素上,而不是使用无容器绑定。
subModel
with
http://jsfiddle.net/rniemeyer/xYphz/1/