如何使用嵌套循环引用相同的父/祖父对象范围?

Sam*_*ony 5 javascript knockout.js

我有Knockout的嵌套循环.我想引用父"范围"中的某些内容.如果你看到下面我总是想要引用相同的父/祖父母,无论我有多深嵌套循环.我看过"with"绑定,不确定它会对我有帮助.有没有办法可以为特定范围创建一个别名,所以在嵌套循环中我可以参考这个别名,并且仍然可以引用当前循环的范围?

    <!-- Somewhere up there is the "scope" I want to capture -->
    <!-- ko foreach: getPages() -->
        <span data-bind="text: pageName" ></span>
        <button data-bind="click: $parents[1].myFunction()" >Press me</button>
        <!-- ko foreach: categories -->
             <span data-bind="text: categoryName" ></span>
             <button data-bind="click: $parents[2].myFunction()" >Press me</button>
            <!-- ko foreach: questions -->
                <span data-bind="text: questionText" ></span>
                <button data-bind="click: $parents[3].myFunction()" >Press me</button>
             <!-- /ko -->
        <!-- /ko -->
    <!-- /ko -->
Run Code Online (Sandbox Code Playgroud)