2016-08-04 10 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 –

関連する問題