将过滤器传递给控制器​​angularjs中的指令

时间:2018-04-24 19:16:48

标签: angularjs angularjs-directive

我有一个名为myCustomDirective的指令,它带有一个值列表和一个过滤函数。

myapp.directive('myCustomDirective', function ($rootScope) {
     return {
        replace: true,
        restrict: 'E',
        scope: {
            values: "=",
            customfilter: "&"
          },
        templateUrl: '/Scripts/template.html',
        link: function (scope, elm, attr) {
        }
    }
});

在我的指令模板template.html中,我有一个简单的列表,它将被过滤为指令从客户端代码获得的过滤函数。

<tbody>
    <tr ng-repeat="item in values | filter:customFilter>
       <td ng-repeat="key in displaykey">{{item.name}}</td>
    </tr>
</tbody>

在我的控制器中,自定义功能是

$scope.myCustomFilter = function(value)
{
  return function(item)
   {
     if(item.name contains value) return true;
     return false;
    }
}

从客户端代码的HTML中,我想传递指令将过滤列表的值和函数。

<div>
  <my-custom-directive customfilter="myCustomFilter("a")"></my-custom-directive>
</div>

我怎样才能做到对不对?

0 个答案:

没有答案