javascript - 在 ng-repeat 中隐藏和显示

标签 javascript angularjs

我试图让按钮只在点击的那个按钮上切换,但是当我使用 ng-repeat 时,它会一起改变。我如何修复它以便它只会在单击时更改?

HTML:

        <ul>
            <li class="displaySubCategory" ng-repeat="communityTheme in community | startFrom:currentPage*pageSize | limitTo:pageSize">
              <div class="categoryImg">
                <img src="img/csvIcon.png" />
                <img src="img/shpIcon.png" />
              </div>
              <div class="categoryDesc">
                <p>{{communityTheme.THEMENAME}}</p>
                <a ng-href="https://assets.onemap.sg/shp/{{SHPFile}}" ng-click="getSHP(communityTheme.QUERYNAME)" target="_blank" download>SHP</a> |
                <a ng-href="https://assets.onemap.sg/kml/{{KMLFile}}" ng-click="getKML(communityTheme.QUERYNAME)" target="_blank" download>KML</a> |
                <a href="" ng-show="viewMarker" ng-click="getMapData(communityTheme.QUERYNAME)">View on Map</a>
                <a href="" ng-hide="viewMarker" ng-click="getMapData(communityTheme.QUERYNAME)">Remove Marker</a>
                <!-- <a href="" ng-click="getData(communityTheme.QUERYNAME)" download>View Data</a> -->
              </div>
            </li>
        </ul>

JS:

        $scope.viewMarker = true;
        $scope.getMapData = function (msg) {
        $scope.viewMarker = !$scope.viewMarker;
        }

之前:

enter image description here

之后:

enter image description here

修改后的代码:

            $scope.viewMarker = true;
            $scope.getMapData = function (msg, passedIndex) {
              if($scope.community[passedIndex].visibility)
              {
                $scope.community[passedIndex].visibility =false;
              }  else {
                $scope.community[passedIndex].visibility = true;
              }

              $scope.viewMarker = !$scope.viewMarker;
<ul>
     <li class="displaySubCategory" ng-repeat="communityTheme in community | startFrom:currentPage*pageSize | limitTo:pageSize">
       <div class="categoryImg">
         <img src="img/csvIcon.png" />
         <img src="img/shpIcon.png" />
       </div>
       <div class="categoryDesc">
         <p>{{communityTheme.THEMENAME}}</p>
         <a ng-href="https://assets.onemap.sg/shp/{{SHPFile}}" ng-click="getSHP(communityTheme.QUERYNAME)" target="_blank" download>SHP</a> |
         <a ng-href="https://assets.onemap.sg/kml/{{KMLFile}}" ng-click="getKML(communityTheme.QUERYNAME)" target="_blank" download>KML</a> |
         <a href="" ng-show="viewMarker" ng-click="getMapData(communityTheme.QUERYNAME, $index)">View on Map</a>
         <a href="" ng-hide="viewMarker" ng-click="getMapData(communityTheme.QUERYNAME, $index)">Remove Marker</a>
         <!-- <a href="" ng-click="getData(communityTheme.QUERYNAME)" download>View Data</a> -->
       </div>
     </li>
 </ul>

最佳答案

这应该有助于澄清...

        var app = angular.module("test", []); 
        app.controller("myCtrl", function($scope) {
            $scope.community  = [
                { THEMENAME:"Milk", QUERYNAME:"Milk", visibility:true}
                , { THEMENAME:"Bread", QUERYNAME:"Milk", visibility:true}
                , { THEMENAME:"Cheese", QUERYNAME:"Milk", visibility:true}
                ];

            $scope.getMapData = function(passedQueryName, passedIndex){
                /*do what you were doing, just add this one more point*/
                
                if($scope.community[passedIndex].visibility) {$scope.community[passedIndex].visibility =false;}
                else {$scope.community[passedIndex].visibility = true;}

            }

        });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>  


<div ng-app="test">


                
                <div ng-app="myShoppingList" ng-controller="myCtrl">
                  
                    <div ng-repeat="communityTheme in community ">
                        {{x}}
                        <div class="categoryDesc">
                            <p>{{communityTheme.THEMENAME}} @ {{$index}}</p>
                            <a ng-href="https://assets.onemap.sg/shp/{{SHPFile}}" ng-click="getSHP(communityTheme.QUERYNAME)" target="_blank" download>SHP</a> |
                            <a ng-href="https://assets.onemap.sg/kml/{{KMLFile}}" ng-click="getKML(communityTheme.QUERYNAME)" target="_blank" download>KML</a> |
                            <a href="" ng-show="communityTheme.visibility" ng-click="getMapData(communityTheme.QUERYNAME, $index)">View on Map</a>
                            <a href="" ng-hide="communityTheme.visibility" ng-click="getMapData(communityTheme.QUERYNAME, $index)">Remove Marker</a>
                            <!-- <a href="" ng-click="getData(communityTheme.QUERYNAME)" download>View Data</a> -->
                        </div>

                    </div>
                  
                </div>

                <p>So far we have made an HTML list based on the items of an array.</p>
    </div>

关于javascript - 在 ng-repeat 中隐藏和显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53077838/

相关文章:

javascript - 在 AngularJS 中使用嵌套 View 时如何保留引用?

javascript - Angular : Texbox value not showing

javascript - 在 Javascript 和 AngularJS 中查找数组中的值

javascript - iframe再次加载失败

javascript - 如何在 JavaScript 中调用 Id?

javascript - 将动态变量注入(inject)到 React Native 的 WebView 的注入(inject)的 JavaScript 字符串中

angularjs - 错误: Future resolved more than once

html - 如何使工具提示 div 根据屏幕的哪一侧改变一侧? ( Angular )

php - onclick ="$.post(this.href); return false;"无法正常工作

javascript - Node express : Requests are taking Top-level domain as the subdomain ? ?