0

我正在使用 require.js 来模块化代码并收到以下错误:

angular.js:68 Uncaught Error: [$injector:unpr] Unknown provider: $filterProvider <- $filter <- srvcConverter

这是服务模块:

define('services/converter', ['angular'], function(angular) {

    var moduleName = 'mdlConverter',

     angular.module(moduleName, []).service('srvcConverter', ['$filter', function($filter) {
        this.formatDateTime = function(value) {
            if (value) {
                return $filter('date')(new Date(value), 'medium');
            }
        };
    }]);
    return moduleName;
});

这是工厂模块:

define('factories/utils', ['angular', 'factories/validator'], function(angular, fcValidator) {

    var moduleName = 'mdlUtils';

    angular.module(moduleName, [fcValidator]).factory("fctrUtils", ['fctrValidator', function(fctrValidator) {

        return {
            'capitalize' : function(str) {
                return typeof str === 'string' && str.length ? (str.charAt(0).toUpperCase() + str.slice(1)) : str;
            }
        };
    }]);
    return moduleName;
});

下面一个是调用者模块:

 define(['app', 'services/converter', 'factories/utils'], function(app, converterService, utils) {

    var utils = angular.injector([utils]).get('fctrUtils'); // This works fine.
    var converter = angular.injector([converterService]).get('srvcConverter'); // This throws mentioned error

    function TableController($scope) {
        $scope.draw = function() {}
    }
    app.register.controller('TableController', TableController);

    return TableController;
});
4

1 回答 1

1

'$filter' 依赖导致了这个问题。

只需添加'ng'as 模块依赖项即可解决问题。

angular.module(moduleName, ['ng']).service('srvcConverter', ['$filter', function($filter) {

}]);
于 2016-06-16T12:21:21.373 回答