有没有办法有条件地将数据从 `ng-repeat` 传递到其他元素?
Is there a way to pass data from `ng-repeat` to other element conditionally?
我正在遍历一个数组,条件是我找到一个值。但是如何将该条件值传递给另一个元素呢?
示例:
<h1>{{title}}</h1> //how to get here?
<ul class="naviPrCatgInfo">
<li ng-repeat='subProject in viewProject.SubProjectIds' ng-class="{'selected' : subProject.Id === subProjectId;}" > //how to pass?
<a ng-class="subProject.Name | trimSpace" ng-href="" ng-click='changeSubProCat(subProject)'>
{{subProject.Name}} {{subProject.Id}}
</a>
</li>
</ul>
不要在 ng-repeat
内声明新变量,因为它将添加到 ng-repeat
.
创建的 child 范围内
您需要将 subProjectId
保留在 ng-repeat
范围之外,以便可以在控制器内部使用,您需要使用 $parent
注释将该变量放在控制器范围内比 ng-repeat
.
因此您可以根据 subProjectId
轻松过滤 viewProject.SubProjectIds
并在那里显示标题元素。
标记
<h1>{{(viewProject.SubProjectIds| filter: {Id: subProjectId}: true)[0].Name}}</h1>
<ul class="naviPrCatgInfo">
<li ng-repeat='subProject in viewProject.SubProjectIds' ng-click="$parent.subProjectId = subProject.Id"
ng-class="{'selected' : subProject.Id === subProjectId}">
<a ng-class="subProject.Name | trimSpace" ng-href="" ng-click='changeSubProCat(subProject)'>
{{subProject.Name}} {{subProject.Id}}
</a>
</li>
</ul>
我正在遍历一个数组,条件是我找到一个值。但是如何将该条件值传递给另一个元素呢?
示例:
<h1>{{title}}</h1> //how to get here?
<ul class="naviPrCatgInfo">
<li ng-repeat='subProject in viewProject.SubProjectIds' ng-class="{'selected' : subProject.Id === subProjectId;}" > //how to pass?
<a ng-class="subProject.Name | trimSpace" ng-href="" ng-click='changeSubProCat(subProject)'>
{{subProject.Name}} {{subProject.Id}}
</a>
</li>
</ul>
不要在 ng-repeat
内声明新变量,因为它将添加到 ng-repeat
.
您需要将 subProjectId
保留在 ng-repeat
范围之外,以便可以在控制器内部使用,您需要使用 $parent
注释将该变量放在控制器范围内比 ng-repeat
.
因此您可以根据 subProjectId
轻松过滤 viewProject.SubProjectIds
并在那里显示标题元素。
标记
<h1>{{(viewProject.SubProjectIds| filter: {Id: subProjectId}: true)[0].Name}}</h1>
<ul class="naviPrCatgInfo">
<li ng-repeat='subProject in viewProject.SubProjectIds' ng-click="$parent.subProjectId = subProject.Id"
ng-class="{'selected' : subProject.Id === subProjectId}">
<a ng-class="subProject.Name | trimSpace" ng-href="" ng-click='changeSubProCat(subProject)'>
{{subProject.Name}} {{subProject.Id}}
</a>
</li>
</ul>