1

如果我有类似的嵌套页面

<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}); 
}

如何从内页的视图模型访问外页的视图模型?

4

1 回答 1

3
<span data-bind="text:$root.topVMProperty"></span>

这里的链接解释了淘汰赛的背景

http://knockoutjs.com/documentation/binding-context.html

根据您的示例,您也可以这样做:

<span data-bind="text:$parent[1].topVMProperty"></span>
于 2013-09-24T20:28:12.083 回答