AngularJS无法将工厂加载到控制器中

时间:2016-11-14 10:32:07

标签: angularjs angular-factory

我有一个名为timer的模块,其中包含了我的控制器和服务。 这是我的module.js文件。

(function () {
  'use strict';

  angular.module('timesheet.timer',[
      'timesheet.timer.controllers',
      'timesheet.timer.services'
  ]).filter('digits', function() {
    return function(input) {
       if (input < 10) input = '0' + input;

      return input;
    }
  });

      angular.module('timesheet.timer.controllers',[]);
      angular.module('timesheet.timer.services',[]);
 })();

过滤器是我需要的自定义过滤器,忽略它。

在我的服务中我有

(function () {
    'use strict';

    angular
        .module('timesheet.timer.services')
        .factory('Timer', Timer);

    Timer.$inject = ['$http'];

    function Timer($http){
        var timer = {
            get: get
        };
        return timer;

       function get(dateString,emp_id){
             return $http.get("/timesheet/employee/"+emp_id+"/tasks/?date=" + dateString);
        }
    }

});

我的主要app.js看起来像这样。

(function () {
  'use strict';

  angular
    .module('timesheet', [
      'angular.filter',
      'chart.js',
        'ngCookies',
         'ngStorage',
      'ui.bootstrap',
      'daterangepicker',
      'xeditable',
      'timesheet.routes',
      'timesheet.timer',
      'timesheet.authentication'


    ]).factory('httpRequestInterceptor',['$cookies', function ($cookies) {
        return {
            request: function (config) {
                var token = $cookies.get('Token');
                if(token) {
                    config.headers['token'] = token;
                }
                return config;
            }
        };
    }]).config(['$httpProvider',function($httpProvider) {
       $httpProvider.interceptors.push('httpRequestInterceptor');
      }])
      .run(function(editableOptions) {
         editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'

    });

  angular
    .module('timesheet.routes', ['ngRoute']);
})();

只要在控制器中添加Timer作为依赖项,我就会收到错误错误:$ injector:unpr 未知提供商

我的控制器是这样的

    (function () {

        angular.module('timesheet.timer.controllers')
            .controller('ManualTableController',ManualTableController);

        ManualTableController.$inject=['$scope','$filter','$http','$uibModal','$rootScope','$filter','Timer'];

        function ManualTableController( $scope, $filter, $http, $uibModal, $rootScope){

            $rootScope.totalDuration = "";
            $scope.noTask = false;
            $scope.error = false;
            $rootScope.tasks = "";
            var emp_id = $rootScope.emp_id;
            $scope.dateString = getDateString($scope.dt);
            $scope.date = angular.copy($scope.dt);

            Timer.get($scope.dateString,emp_id).then(timerSuccessFn, timerErrorFn);
            function timerSuccessFn(data, status, headers, config) {
            console.log(data);
           }
}
});   

我做错了什么?为什么工厂没有注入我的控制器。我还在base.html中包含了所有JS文件

2 个答案:

答案 0 :(得分:0)

我认为您必须调用您的服务:

})();

答案 1 :(得分:0)

注射时出现错误:

ManualTableController.$inject=['$scope','$filter','$http','$uibModal','$rootScope',
'$filter','Timer'];

function ManualTableController( $scope, $filter, $http, $uibModal, 
$rootScope, ){ //you missed the injection here
..
}

你忘记在你的控制器中注入Timer,并且$ filter被注入两次。

相关问题