【问题标题】:$stateProvider loading service object is not defined$stateProvider 加载服务对象未定义
【发布时间】:2016-03-14 07:04:18
【问题描述】:

我有一个服务:

      angular.module('TestApp')
      .service('Test', ['$http', '$localStorage', function($http, $localStorage) {
        var baseUrl = "http://test.com";

        return {
            tests: function(success, error) {
                $http.get(baseUrl + '/tests').success(success).error(error)
            }
        } 
      }]);

我有一个控制器:

angular.module('TestApp')
  .controller('TestController',  ['$rootScope', '$scope', Test, function($rootScope, $scope, Test) {

    Merchant.terminal(function(res) {
        $scope.tests= res;
    }, function() {
        $rootScope.error = 'Failed to fetch details';
    }); 
 }]);

然后在 app.js 中我使用 resolve 来获取文件:

$stateProvider.state('test', {
           url: "/test", 
           templateUrl: "views/test.html",
           data: {pageTitle: 'Test Search'},
           controller: "TestController",
           resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'TestApp',
                        insertBefore: '#ng_load_plugins_before', 
                        files: [
                            'scripts/controllers/TestController.js',
                            'scripts/services/testService.js',
                        ] 
                    });
                }]
            }
       });

但我收到此错误:

Uncaught ReferenceError: Test is not defined

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    尝试更改您的控制器定义:

    angular.module('TestApp')
       .controller('TestController',  ['$rootScope', '$scope', Test,
    function($rootScope, $scope, Test) { ... }
    

    到:

    angular.module('TestApp')
       .controller('TestController',  ['$rootScope', '$scope', 'Test',
    function($rootScope, $scope, Test) { ... }
    

    注意 dep 注入列表中的“测试”...

    【讨论】:

    • 大声笑,所以我会为所有人投上一票,并为最早和完整的答案打勾。
    【解决方案2】:

    您的控制器中缺少一个 '' while name Service ,因为它是一个字符串。

    来自

    angular.module('TestApp')
      .controller('TestController',  ['$rootScope', '$scope', Test, function($rootScope, $scope, Test)
    

    收件人

    angular.module('TestApp')
      .controller('TestController',  ['$rootScope', '$scope', 'Test', function($rootScope, $scope, Test) 
    

    还要在加载控制器之前将服务排序

    'scripts/services/testService.js'
    'scripts/controllers/TestController.js'
    

    【讨论】:

      【解决方案3】:

      将服务文件放在控制器之前

      'scripts/services/testService.js',
      'scripts/controllers/TestController.js'
      

      Injector 应该是一个字符串

      ['$rootScope', '$scope', 'Test', function
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        • 1970-01-01
        • 2023-01-11
        • 2020-06-02
        • 1970-01-01
        • 2021-09-29
        • 2016-03-25
        相关资源
        最近更新 更多