2016-08-04 2 views
3

ng-repeat를 사용하여 목록 내부에 데이터를 채 웁니다.AngularJs : 배열 푸시가 다른 요소를 대체합니다.

HTML - 후 임시 변수를 만들어 형태에서의 값을 저장

<div ng-repeat="data in GCAList|filter:year" class="listGCA" ng-click="select()"> 
    <div class="listLibraryName"> 
     {{ data.LibraryName }} 
    </div> 
    <div class="listProjects"> 
     {{ data.Projects }} 
    </div> 
    <div class="listStatus"> 
     {{ data.Status }} 
    </div> 
    <div class="listYear"> 
     {{data.TaxYear}} 
    </div> 
</div> 

배열로 밀어. 이제 푸쉬하는 동안 문제는 $$ hashkey : "object : 81"이 자동으로 생성되고 다음 푸시를 위해 동일한 해시 키가 생성되고 배열에 이전에 삽입 된 데이터를 대체합니다.

스크립트 파일을 교체하는 일이 없도록 도움이되지 않았다 $ 지수에 의해 트랙 추가

ng-repeat="data in GCAList|filter:year track by $index" : 난에 '$ 인덱스 트랙'을 사용 당신은을 사용하기 때문에

var app = angular.module("Accounts", ['restangular', 'ngDialog', 'ngAnimate']) 
var temp = { 
    "id": 0, 
    "LibraryName": "" , 
    "Projects": 0, 
    "Status": "", 
    "TaxYear": 0 
}; 

app.controller("accountsController", function($scope, ngDialog, RestRepository) { 
    $scope.showEGCA = false; 
    $scope.showGCA = true; 
    $scope.name = ""; 
    RestRepository.getJson().then(function(response) { 
     $scope.dataList = response; 
     console.log($scope.dataList); 
     $scope.GCAList = $scope.dataList[0]; 
     console.log($scope.GCAList); 
     $scope.EGCAList = $scope.dataList[1]; 
     console.log($scope.EGCAList); 
    }); 
    $scope.popOpen = function() { 
     ngDialog.open({ 
      template: 'Pop Up.html', 
      scope: $scope, 
      controller: function($scope) { 
       $scope.cancelCOA = function() { 
        ngDialog.close(); 
       }; 
       $scope.createGCA = function() { 
        temp.id = $scope.GCAList.length + 1; 
        temp.LibraryName = $scope.name; 
        temp.Projects = 2; 
        temp.Status = "Inactive"; 
        temp.TaxYear = $scope.taxYear; 
        console.log(temp); 
        $scope.GCAList.push(temp); 
        console.log($scope.GCAList); 
        ngDialog.close(); 
       }; 
      }, 
      closeByDocument: false, 
      closeByEscape: false, 
      showClose: false, 
     }); 
    }; 
    $scope.gcaOpen = function() { 
     $scope.showGCA = !$scope.showGCA; 
     $scope.showEGCA = true; 
    }; 
    $scope.egcaOpen = function() { 
     $scope.showEGCA = !$scope.showEGCA; 
     $scope.showGCA = true; 
    }; 
}); 

app.config(function(RestangularProvider) { 
    RestangularProvider.setBaseUrl('http://10.198.50.19:98/jsonData/'); 
}); 

app.factory("RestRepository", [ 
    'Restangular', function(Restangular) { 
     return { 
      getJson: function() { 
       return Restangular.one('jsonData.json').get(); 
      } 
     } 
    } 
]); 
+1

'temp' 모든 푸시를위한 새로운 객체 여야합니다 : 당신이 동일한 개체 속성을 변경하고 배열에 아이디어 @fantarama에 대한 – fantarama

+0

감사를 동일한 인스턴스를 추가! :) 그것은 일했다 .. –

답변

1

이유는있다 함수 호출 내에서 매번 위에 생성 된 동일한 객체가 동일한 해시 키 값을 얻고 동일한 인스턴스를 배열에 추가합니다. 제거,

var temp ={ 
     "id":0, 
     "LibraryName":"" , 
     "Projects":0, 
     "Status":"", 
     "TaxYear":0 
     }; 

그리고 createGCA() 안에 배치하십시오. 코드는, 모양을

$scope.createGCA = function() { 
       $scope.temp ={ 
      "id":0, 
      "LibraryName":"" , 
      "Projects":0, 
      "Status":"", 
      "TaxYear":0 
        }; 
       $scope.temp.id=$scope.GCAList.length+1; 
       $scope.temp.LibraryName=$scope.name; 
       $scope.temp.Projects=2; 
       $scope.temp.Status="Inactive"; 
       $scope.temp.TaxYear=$scope.taxYear; 
       console.log($scope.temp); 
       $scope.GCAList.push($scope.temp); 
       console.log($scope.GCAList); 
       ngDialog.close(); 
      }; 
+0

그 일한 @ KishoreL 고맙습니다. : D –

관련 문제