在角度推动时生成新的$$ hashkey

时间:2016-07-01 22:28:19

标签: angularjs

我想在我的数组对象中推送一个项目,这是我的代码:

<li gridster-item="item" ng-repeat="item in widgets" style="border: #ddd 1px solid;">
   <a ng-click="list.push(item)" style="padding:2px;position: absolute;z-index: 1000;right:2%;top: 2%;" class="add-widget-to-sidebar pull-right">
        <span class="glyphicon glyphicon-plus-sign" aria-hidden="true" style="margin:2px 10px;"></span>
    </a></li>

和$ scope.widgets已经在每个对象上都有hashkey,我想要做的是每次在列表中推送一个新项目时,我都需要一个新的$$ hashkey。我已经尝试过这样做但它没有工作:

$scope.$watchCollection('list', function() {
        $scope.temp = []
        rand=0;
        angular.forEach($scope.list,function(value,key){
            value.$$hashKey = rand++;
            console.log(rand);
            $scope.temp.push(value);
        }); 
        $scope.list = $scope.temp;
        console.log('temp');
        console.log($scope.temp);
        console.log('list');
        console.log($scope.list);
        $scope.resize(); 
    });

1 个答案:

答案 0 :(得分:1)

我找到了另一种解决方案:

<ul>
  <li ng-repeat="item in widgets track by $id($index)">
    .....
  </li>

这将删除你的ng-repeat

上的$$ hashkey