녹아웃보기에서 $ parent의 $ parent 액세스-중첩 컨텍스트
간결성을 위해 업데이트 됨
$ parents의 $ parent를 중첩 된 Knockout foreach / 바인딩을 사용하여 참조하려면 어떻게해야합니까?
예 -
<!-- ko foreach: grandParent -->
<tr>
<!-- ko foreach: $parent.parents --> // <-- Doesn't work
<!-- ko foreach: children -->
<td data-bind="if: favToy().name == $parent.$parent.favToy().name">
<span data-bind="text: favToy().name"></span>
</td>
<!-- /ko -->
<!-- /ko -->
</tr>
<!-- /ko -->
실물
혼란스러운 질문에 대해 죄송하지만 $ parent의 $ parent의 값 (ugh!)
<!-- ko foreach: grandParent -->
<tr>
<!-- ko foreach: $parent.parents -->
<!-- ko foreach: children -->
<td data-bind="if: favToy().name == $parent.$parent.favToy().name">
<span data-bind="text: favToy().name"></span>
</td>
<!-- /ko -->
<!-- /ko -->
</tr>
<!-- /ko -->
이런 식으로하는 것이 더 쉬울 것이지만 내가 읽은 것에서 이것은 불가능하거나 잘못하고 있습니다. :)
<!-- ko foreach: grandParent -->
<tr>
<!-- ko foreach: $parent.parents -->
<!-- ko foreach: children ? favToy().name == $parent.$parent.favToy().name -->
<td data-bind="text: favToy().name"></td>
<!-- /ko -->
<!-- /ko -->
</tr>
<!-- /ko -->
어떤 도움이라도 대단히 감사하겠습니다.
$parents배열을 사용하면 조부모는 $parents[1]. 예제 $root의 grandParent객체가 최상위 부모 인 경우 에도 사용할 수 있습니다 .
로부터 문서 :
$ 부모
이것은 모든 상위 뷰 모델을 나타내는 배열입니다.
$ parents [0]은 부모 컨텍스트의 뷰 모델입니다 (즉, $ parent와 동일).
$ parents [1]은 조부모 컨텍스트의 뷰 모델입니다.
$ parents [2]는 증조부모 컨텍스트의 뷰 모델입니다.
… 등등.
$ root
이것은 루트 컨텍스트, 즉 최상위 상위 컨텍스트의 기본 뷰 모델 객체입니다. 일반적으로 ko.applyBindings에 전달 된 개체입니다. $ parents [$ parents.length-1]과 동일합니다.
사용할 수 있습니다 $parentContext.$parent.
$parentContext같은 많은 유용한 특성을 제공한다 ( $data, $parent, $index, ...)
다음과 같이 noChildContext 설정을 사용하는 것이 더 쉬울 것이라고 생각합니다.
자식 컨텍스트를 만들지 않고 "as"사용
The default behavior of the as option is to add a name for the current item while still also binding the contents to the item. But you may prefer keep the context unchanged and only set the name of the current item. This latter behavior will probably be the default in a future version of Knockout. To turn it on for a specific binding, set the noChildContext option to true. When this option is used along with as, all access to the array items must be through the given name, and $data will remain set to the outer viewmodel. For example:
<ul data-bind="foreach: { data: categories, as: 'category', noChildContext: true }">
<li>
<ul data-bind="foreach: { data: category.items, as: 'item', noChildContext: true }">
<li>
<span data-bind="text: category.name"></span>:
<span data-bind="text: item"></span>
</li>
</ul>
</li>
</ul>
참고URL : https://stackoverflow.com/questions/17069381/access-parents-parent-in-knockout-view-nesting-context
'developer tip' 카테고리의 다른 글
| msbuild로 exec 작업 출력을 얻는 방법 (0) | 2020.10.19 |
|---|---|
| sql_mode의 특정 값을 어떻게 볼 수 있습니까? (0) | 2020.10.19 |
| Scala의 명시 적 유형 변환 (0) | 2020.10.19 |
| jQuery 변경 이벤트 (0) | 2020.10.19 |
| Jekyll-메뉴 모음에서 현재 탭을 자동으로 강조 표시 (0) | 2020.10.18 |