【问题标题】:Angularjs: How to add custom filter to directive?Angularjs:如何将自定义过滤器添加到指令中?
【发布时间】:2015-01-19 08:38:22
【问题描述】:

我的代码:

        angular.module('app.filters').filter('someFilter', function()
        {
           //some code here
        });
        angular.module('app.directives').directive('myDirective', function() {
           //some code...
            scope: false,
            controller: ['$scope',function($scope, $filter) {
                $scope.evaluate = function (attr) {
                    return = $scope.$eval(attr);
                };
            }],
         });

        angular.module('app',['app.directives', 'app.filters']);

问题是当我在 "'some_val'|someFilter" 之类的属性上调用 scope.evaluate(attr) 函数时,我收到 [$injector:unpr] Unknown provider: someFilterFilterProvider <- someFilterFilter

在我的 html 中没问题:<html ng-app='app'>...etc

【问题讨论】:

    标签: angularjs angular-directive angular-filters


    【解决方案1】:

    angular.module('app.filters') 和 angular.module('app.directives') 是两个不同的模块,所以你只需要使用“app”就可以了。

    angular.module('app').filter()... angular.module("app").directive()....

    【讨论】:

    • 但是它们被注入到主模块“app”中,像 html 中的 {{'some_val'|someFilter}} 可以正常工作!在我的指令中,我使用了父范围,它知道过滤器。是的,我不能改变这个结构
    猜你喜欢
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2018-06-02
    相关资源
    最近更新 更多