简体   繁体   English

AngularJS:更改特定ng-repeat项目的ng-if值?

[英]AngularJS: change the ng-if value of a specific ng-repeat item?

I am new to angular and javascript for the most part, so this may be a general javascript question: 我是角度和javascript的新手,所以这可能是一般的javascript问题:

I have a list of divs created through ng-repeat and each div contains an ng-if="expanded == true" that is made true by clicking a button and running the below function. 我有一个通过ng-repeat创建的div列表,每个div包含一个ng-if =“expanded == true”,通过单击按钮并运行以下函数使其成为现实。

How do I change the ng-if value to true for only that specific ng-if inside that div? 如果在该div内部,如何仅将ng-if值更改为true? The current set up changes expanded to equal true and all of the ng-if material then shows. 当前设置的更改扩展为相等,然后显示所有 ng-if材料。

//HTML

    <div class="panel-body">
        <span ng-if="expanded == false">This property is missing several documents. Click <a ng-click="buyChecklist()">here</a> to complete this profile.</span>
    </div>  

    <ul class="list-group" ng-if="expanded == true">
        <li class="list-group-item text-right" ng-repeat="reqs in buyReqs">
            <span class="pull-left">{{reqs.name}}</span>
            <span><a class="btn btn-sm btn-primary" href="">Upload</a></span>
        </li>
    </ul>

//Controller.js 

    $scope.buyChecklist = function() {
        $scope.expanded = true; 
    }

Since you want to change an expanded property for a particular div, this is an indication that you actually need an expanded state per div and not just a single expanded state. 由于您想要更改特定div的扩展属性,这表明您实际上需要每个div的扩展状态而不仅仅是单个扩展状态。 So you need an array instead: 所以你需要一个数组:

expanded = [false, false, false, false]; //one per div

Then in your html template, you check the expanded index of the particular div using the current index of the ng-repeat loop: 然后在你的html模板中,使用ng-repeat循环的当前索引检查特定div的扩展索引:

<span ng-if="expanded[$index]"> .. </span>

Notice the use of the $index value. 注意使用$index值。 This variable is available to you within a ng-repeat loop. 在ng-repeat循环中可以使用此变量。

The documentation of ng-repeat mentions this and other variables available to you within the loop (ie $first, $last, $even, $odd, etc.) ng-repeat文档提到了循环中可用的这个和其他变量(即$ first,$ last,$ even,$ odd等)

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

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