Angularjs Scope method not being called by ng-class

荒凉一梦 提交于 2019-12-05 21:07:10

Pass the getCellColor method to the directive like this:

<count-table table-type='tableType' 
get-cell-color='getCellColor(col)'>
</count-table>

The directive looks like this:

app.directive('countTable', function(){
  return {
    restrict: 'E',
    scope: {
      tableType:'=tableType',
      getCellColor: '&'
    },
    templateUrl: 'table.html'
  };
});

The directive template looks like this:

 <table>
   <tr ng-repeat="row in tableType.data.rows">
      <td ng-repeat="col in row track by $index" ng-class="getCellColor({col: col})">{{col}}</td>
   </tr>
 </table>

Plunker

use getCellColor(col) instead of getCellColor({{col}})

ng-class use $scope.$eval() to evaluate an expression. you don't need to use a pair of brackets

Update: Although you can use getCellColor(col) method to change class. It can just be evaluated once. It's suggested to set a property to each col.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!