簡體   English   中英

Angularjs ng-repeat 傳遞的索引未定義

[英]Angularjs ng-repeat passed index not defined

我試圖在 ng-repeat 中打開和關閉按鈕,地圖上的視圖有效,但是當它更改為刪除標記時,它在控制台日志中向我傳遞錯誤“ReferenceError:passedIndex 未定義”。 有什么辦法可以解決嗎?

HTML:

            <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 href="" ng-hide="communityTheme.visibility" ng-click="getMapData(communityTheme.QUERYNAME, $index)">View on Map</a>
                <a href="" ng-show="communityTheme.visibility" ng-click="removeMarker(communityTheme.QUERYNAME, $index)">Remove Marker</a>
              </div>
            </li>

JS:

        $scope.getMapData = function (msg, passedIndex) {
                map.addLayer(cities);

                $scope.Lng.length = 0;
                $scope.Lat.length = 0;
                $scope.dataLatLng.length = 0;

               queryNameUrl = 'https://developers.onemap.sg/publicapi/themeapi/retrieveTheme?queryName=' + msg +
               '&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjMsInVzZXJfaWQiOjMsImVtYWlsIjoicHVibGljQXBpUm9sZUBzbGEuZ292LnNnIiwiZm9yZXZlciI6ZmFsc2UsImlzcyI6Imh0dHA6XC9cL29tMi5kZmUub25lbWFwLnNnXC9hcGlcL3YyXC91c2VyXC9zZXNzaW9uIiwiaWF0IjoxNTQwOTI5OTE2LCJleHAiOjE1NDEzNjE5MTYsIm5iZiI6MTU0MDkyOTkxNiwianRpIjoiYjVkNmZkNGJhOWJiNGJiM2FkNWQzN2ZhNTAzMGIxYWEifQ.YQdfV43wrg8dX-He7-mwIL2Qhjsexq0tgNu5RotAdu4';
               $http.get(queryNameUrl).then(function(response) {
                 $scope.apiResult = response.data.SrchResults;
                 $scope.apiResult.splice(0,1);
                 console.log($scope.apiResult)

                       for (var i= 0; i < $scope.apiResult.length; i++) {
                            if ($scope.apiResult[i].Type == "Point"){
                              $scope.apiResult[i].visibility = true;
                              console.log($scope.apiResult)
                              $scope.dataLatLng.push($scope.apiResult[i].LatLng)
                              $scope.Lat.push($scope.dataLatLng[i].split(',')[0]);
                              $scope.Lng.push($scope.dataLatLng[i].split(',')[1]);
                              L.marker([$scope.Lat[i], $scope.Lng[i]], {icon: greenIcon}).bindPopup($scope.apiResult[i].DESCRIPTION).addTo(cities);
                            }
                              // else if ($scope.apiResult[i].Type == "Polygon"){
                              //       $scope.PolyLine.push($scope.apiResult[i].LatLng)
                              //       console.log($scope.PolyLine)
                              //       // for (var i = 0; i < $scope.PolyLine.length; i++) {
                              //       //     $scope.polyLineCord.push($scope.PolyLine[i])
                              //       //     // console.log($scope.polyLineCord)
                              //       // }
                              //   }
                       }
               })
               if($scope.community[passedIndex].visibility)
                {
                  $scope.community[passedIndex].visibility = false;
                }
               else{
                 $scope.community[passedIndex].visibility = true;
               }
        }

刪除標記:

        $scope.removeMarker = function ($index) {

            if($scope.community[passedIndex].visibility)
             {
               $scope.community[passedIndex].visibility =false;
               cities.clearLayers();
             }
            else {
              $scope.community[passedIndex].visibility = true;
            }
        }

我在這里先向您的幫助表示感謝 !

在此處輸入圖片說明

您需要通過 $index 使用 track

 <li class="displaySubCategory" ng-repeat="communityTheme in community | startFrom:currentPage*pageSize | limitTo:pageSize track by $index">

它應該是passedIndex代替$index在removeMarker功能

$scope.removeMarker = function (passedIndex) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM