angular.injector(['moduleName'])。get('name')适用于Factory而不适用于Service

时间:2016-06-16 09:36:46

标签: javascript angularjs angular-controller angularjs-injector

我正在使用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;
});

1 个答案:

答案 0 :(得分:1)

&#39; $滤波器&#39;依赖导致了这个问题。

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

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

}]);