angularjs – 如何在angular指令的link函数中传递$filter

前端之家收集整理的这篇文章主要介绍了angularjs – 如何在angular指令的link函数中传递$filter前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在angular指令的link函数中使用$filter,但是没有办法将$filter作为参数传递给link函数.

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

如何访问$filter里面的链接功能

解决方法

只需将它注入到实际的指令函数中,然后它就可以在整个指令中使用(包括你的链接函数).

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

        }
    };
});

只需将链接函数视为私有指令函数,它不直接处理角度的注入系统.通过注入主指令函数,你基本上就是说所有内部函数都可以使用它.

猜你在找的Angularjs相关文章