简体   繁体   English

有没有办法将数据从ng-repeat有条件地传递到其他元素?

[英]Is there a way to pass data from `ng-repeat` to other element conditionally?

I am looping over an array, by the conditional I am finding a value. 我遍历一个数组,条件是我找到一个值。 But how to pass that conditional value to another element? 但是,如何将该条件值传递给另一个元素?

Example : 范例:

<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>

Don't declare the new variable inside ng-repeat , because that will get added inside the child scope created by the ng-repeat . 不要在ng-repeat声明新变量,因为它将被添加到ng-repeat创建的子范围内。

You need to keep subProjectId outside the ng-repeat scope so that could be available inside the controller, you need to use $parent anotation to place that variable inside the controller scope rather than the ng-repeat . 您需要将subProjectId保留在ng-repeat范围之外,以便可以在控制器内部使用,您需要使用$parent注释将该变量放置在控制器范围内,而不是ng-repeat

So you can easily filter viewProject.SubProjectIds on the basis of subProjectId and show the title element there. 因此,您可以基于subProjectId轻松过滤viewProject.SubProjectIds并在subProjectId显示title元素。

Markup 标记

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM