在我的模块中使用$ state时出现问题

时间:2016-07-31 15:43:38

标签: angularjs angular-ui-router

我正在尝试使用$ state服务来加载我定义的特定状态。问题是我在尝试这样做时遇到了各种错误。我已经阅读了循环依赖关系,并尝试了stackoverflow的其他线程中提供的方法,如使用:

  

$ injector.get($状态)

但它也失败了,我得到了未知的提供商$状态错误。希望有人可以帮助我...

这是我的代码:

'use strict';

angular.module("home", ['ui.router', 'home.controllers', 'urlLanguageStorage', 'pascalprecht.translate'])

    .config(['$stateProvider', '$translateProvider', '$state', 
             function($stateProvider, $translateProvider, $state){

        $translateProvider.useUrlLoader("home/mensajes");
        $translateProvider.useStorage("UrlLanguageStorage");
        $translateProvider.preferredLanguage("euk");
        $translateProvider.fallbackLanguage("en");

        $stateProvider
                .state("introduccion", {
                    url : "/prueba",
                    templateUrl : "resources/js/home/views/introduccion.html",
                    controller : "HomeController"
                });

        $state.go('introduccion');
    }]);

'use strict';

angular.module("home.controllers", ['home.services'])

    .controller('HomeController', ['$scope', 'HomeService', function($scope, HomeService) {

              probar();

              function probar(){
                  $scope.texto = HomeService.prueba().get();
              }
    }]);

'use strict';

angular.module('home.services',['ngResource'])

    .factory('HomeService',function($resource){

        return {

            prueba: function() {
                return $resource('http://192.168.11.6:8080/web/home/prueba');
            }
        };
    });

'use strict';

angular.module('urlLanguageStorage',['ngResource'])

    .factory('UrlLanguageStorage', ['$location',function($location){

        return {
            put : function (name, value) {},
            get : function (name) {
                return $location.search()['lang']
            }
        };
    }]);

我得到的错误如下:

  

由于以下原因无法实例化模块主页:错误:[$ injector:unpr]   http://errors.angularjs.org/1.5.5/ $注射器/ unpr?P0 =%24state       在错误(本机)       在https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:6:412       在https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:43:84       在d(https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:40:344)       在e(https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:41:78)       at Object.invoke(https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:41:163)       在d(https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:39:321)       在https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:39:445       在q(https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:7:355)       在g(https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:39:222

1 个答案:

答案 0 :(得分:1)

您与$stateProvider$state混淆了。

$stateProviderProvider$stateService。他们不一样。简短回答:您在$stateProvider阶段使用config,并在控制器中使用$stateProvider

您的错误发生是因为您在$state中注入了.config服务,这是错误的:

.config(['$stateProvider', '$translateProvider', '$state'

删除$state,您没问题:

angular.module("home", ['ui.router', 'home.controllers', 'urlLanguageStorage', 'pascalprecht.translate'])
  //take out $state from config phase
  .config(['$stateProvider', '$translateProvider', '$state',
    function($stateProvider, $translateProvider, $state) {

      $translateProvider.useUrlLoader("home/mensajes");
      $translateProvider.useStorage("UrlLanguageStorage");
      $translateProvider.preferredLanguage("euk");
      $translateProvider.fallbackLanguage("en");

      $stateProvider
        .state("introduccion", {
          url: "/prueba",
          templateUrl: "resources/js/home/views/introduccion.html",
          controller: "HomeController"
        });
      //remove this line, set this in controller
      //$state.go('introduccion');
    }
  ]);