How to pass $ filter inside link function of angular directive

I need $ filter inside the link function of the angular directive, but there is no way to pass $ filter as a parameter to the link function.

app.directive('myDirective', function($compile) {
  return {
    restrict: 'A',
    scope: {
      ngModel: '=',
    },
    require: 'ngModel',
    link: function($scope, elem, attr, ctrl) {

    }
  };
});

http://plnkr.co/edit/XpnY5dq7rnl2sWXlsN4t?p=preview

How to access the $ filter function inside a link?

+4
source share
2 answers

Just enter it into your actual directory function, and then it can be used throughout your directive (including the link function).

app.directive('myDirective', function($compile, $filter){
    return {
        restrict: 'A',
        scope: {
            ngModel: '=',
        },
        require: 'ngModel',
        link: function($scope, elem, attr, ctrl) {
            // call $filter here as you wish

        }
    };
});

, angular. , , .

+10

$filter, /factory angular ($ timeout, $filter)

  app.directive('myDirective', ['$compile','$filter',function($compile,$filter) {
  return {
    restrict: 'A',
    scope: {
      ngModel: '=',
    },
    require: 'ngModel',
    link: function($scope, elem, attr, ctrl) {
      console.log($filter);
    }
  };
}]);

http://plnkr.co/edit/JUE1F83l1BC0LTlO7cxJ?p=preview

+4

Source: https://habr.com/ru/post/1614255/


All Articles