AngularJs:数组推送替换其他元素

AngularJs:数组推送替换其他元素,angularjs,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.Pro

用于填充列表中的数据

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值,然后将相同的实例添加到数组中。除去

   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();
            };

temp
每次推送都必须是一个新对象:您正在更改相同的对象属性,并将相同的实例添加到idea@fantarama!!!:)的数组存储库中它起作用了。。
    $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();
            };