如果我的嵌套页面有这样的东西
<div data-bind="page: {id: 'top', withOnShow: topVM}">
<div data-bind="page: {id: 'sub1', withOnShow: sub1VM}">
<span data-bind="text:sub1Property"></span>
</div>
</div>
topVM = function(callback) { callback({topProperty: XXX}); }
sub1VM = function(callback) {
// how can I access topVM here?
callback({sub1Property: XXX});
}
如何从内页的视图模型访问外页的视图模型?
答案 0 :(得分:3)
<span data-bind="text:$root.topVMProperty"></span>
此处的链接解释了淘汰赛背景
http://knockoutjs.com/documentation/binding-context.html
根据你的例子,你也可以这样做:
<span data-bind="text:$parent[1].topVMProperty"></span>