在 UI-Grid 中调用 cellClass 函数的通用函数

Calling common function for cellClass Function in UI-Grid

我打算为所有列调用相同的函数。我尝试了许多不同的方式来调用函数。 None 成功了。我确定我做了一些愚蠢的事情。如果有人能纠正我。

$scope.gridOptions = {
enableSorting: true,
columnDefs: [
  { field: 'name', 
    //cellClass:  howCanIhaveCommonFunction() 
  },
  { field: 'company',
    //cellClass:  howCanIhaveCommonFunction()

  }
]

};

 // below function this would be common for all columns
  var howCanIhaveCommonFunction = function(grid, row, col, rowRenderIndex, colRenderIndex) 
  {
          if (grid.getCellValue(row,col) === 'Velity' || grid.getCellValue(row,col) === 'Beryl Rice' ) {
            return 'blue';
          }
          return 'red';
  }

http://plnkr.co/edit/4yjVbhmfR47cf7FGfyFk?p=preview

是的,这是一个简单的错误,您没有只说函数名称,而是在没有任何参数的情况下调用它。

$scope.gridOptions = {
enableSorting: true,
columnDefs: [
  { field: 'name', 
    cellClass:  howCanIhaveCommonFunction 
  },
  { field: 'company',
    cellClass:  howCanIhaveCommonFunction

  }
]

你只是通过那些 () 使所有参数为空,只需传递函数名称,ui-grid 将使用正确的参数调用它。