Pass information between directive and controller

2019-09-15 01:01发布

I have the next directive

  angular
  .module('01')
  .directive('tableSortable', tableSortable);

/** @ngInject */
function tableSortable() {
return {
  restrict: 'E',
  transclude: true,
  templateUrl: 'app/components/tabla/tableSortable.html',
  scope: {
    columns: '=',
    data: '=',
    sort: '=',
    onclickRow: '='
  }

and I have the next html that shows a table

<tbody>
<tr  ng-click="click(object)" ng-repeat="object in data | orderBy:sort.column:sort.descending | orderBy:sort.column:sort.descending | startFrom:currentPage*pageSize | limitTo:pageSize">
  <td
    ng-repeat="key in object"
    ng-if="key != '$$hashKey'" >
    {{object[columns[$index].variable] | customFilter : getFilterOfColumn(columns[$index].variable)}}
  </td>
</tr>
 </tbody>

I have this other html where I call the directive

  <table-sortable onclick-row="main.onclickRow(msg)"
    columns="main.columnsBusquedas" data="main.rowsBusquedas" sort="main.sortBusquedas">
    </table-sortable>

And this controller with a function onclickRow(msg) where I want to take the row that the users click on

function onclickRow(msg){
  $log.debug(msg);
}

This code didn't work... Could you help me please?

Thanks.

1条回答
forever°为你锁心
2楼-- · 2019-09-15 01:39

Change your directive part where you bind function, you should use & instead of =

function tableSortable() {
return {
  restrict: 'E',
  transclude: true,
  templateUrl: 'app/components/tabla/tableSortable.html',
  scope: {
    columns: '=',
    data: '=',
    sort: '=',
    click: '&onclickRow' // this how we call function from directive...
  }

In HTML change your ng-click like below..

<tbody>
<tr  ng-click="click({'msg':object})" ng-repeat="object in data | orderBy:sort.column:sort.descending | orderBy:sort.column:sort.descending | startFrom:currentPage*pageSize | limitTo:pageSize">
  <td
    ng-repeat="key in object"
    ng-if="key != '$$hashKey'" >
    {{object[columns[$index].variable] | customFilter : getFilterOfColumn(columns[$index].variable)}}
  </td>
</tr>
 </tbody>
查看更多
登录 后发表回答