在 angularJS UI 网格中的 for 循环中为 gridOption 绑定 onRegisterApi

Binding onRegisterApi for gridOption within for loop in angularJS UI grid

我有一种情况,我在 for 循环中创建 gridOption 并为此绑定 onRegisterApi。但是 onRegisterApi 方法中的循环变量超出了循环变量。代码片段如下:

for(var k=0; k<3; k++){
 $scope.gridOptions[k] = {
      data : $scope.gridData[k],
      enableCellEditOnFocus : true,
      columnDefs : $scope.colmDef[k],
      onRegisterApi : function(gridApi){
           console.log(k) // here value of k is always 2.
           $scope.gridApi[k] = gridApi;
            console.log(k) // here value of k is always 3.
            $scope.gridApi[k].edit.on.afterCellEdit($scope, function(rowEntity, colDef, newValue, oldValue){
                    console.log(k) // here value of k is always 3.
                    /* I want to handle celledit of specific grid from gridApi. But this is not working as the variable 'k' doesn't change according to loop and it will be fixed always.*/
           })
      }

   }

}

所以我想在循环中为 gridOptions 分配 onRegisterApi。请帮我解决这个问题。

我终于可以解决这个问题了。我做了如下更改:

for(var k=0; k<3; k++){
 $scope.gridOptions[k] = {
      data : $scope.gridData[k],
      enableCellEditOnFocus : true,
      columnDefs : $scope.colmDef[k],
      customStoredVar : k,
      onRegisterApi : function(gridApi){
           var gridRef = this;
           gridApi.edit.on.afterCellEdit($scope, function(rowEntity, colDef, newValue, oldValue){
                   var gridArrayRefVar = gridRef.customStoredVar; //this contains respective grid array value. So this value can be used for array ref
           })
      }
   }
}