【问题标题】:Unknown Provider error despite other controllers and services working尽管其他控制器和服务正常工作,但出现未知的提供程序错误
【发布时间】:2015-10-05 02:49:05
【问题描述】:

这就是我为所有模板定义状态、控制器和 url 的 app.js 的样子:

angular.module('starter', ['ionic', 'starter.controllers','starter.services'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    ionic.Platform.fullScreen()
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      // StatusBar.styleDefault();
      StatusBar.hide();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "templates/rubyonic/menu.html",
    controller: 'AppCtrl',
    reload: true
  })

  // .state('login', {
  //   url: "/login",
  //   templateUrl: "templates/rubyonic/login.html",
  //   controller: 'AppCtrl'
  // })

  .state('login', {
        url: '/login',
        templateUrl: "templates/rubyonic/login.html",
        controller: 'LoginCtrl'
    })


  .state('app.alerts', {
    url: "/alerts",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/alerts.html",
        controller: 'AppCtrl'
      }
    }
  })

   .state('app.studies', {
    url: "/studies",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/studies.html",
        controller: 'AppCtrl',
        reload: true
      }
    }
  })

   .state('app.study_collections', {
    url: "/studies/:studynodeRef",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/overview.html",
        controller: 'AppCtrl',
        reload: true
      }
    }
  })



  .state('app.rank-charts', {
    url: "/rank_charts",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/rank_charts.html",
        controller: 'AppCtrl'
      }
    }
  })

  // .state('app.overview', {
  //   url: "/overview",
  //   views: {
  //     'menuContent': {
  //       templateUrl: "templates/rubyonic/overview.html"
  //     }
  //   }
  // })

  .state('app.claim-details', {
    url: "/claim-details",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/claim_details.html",
        controller: 'AppCtrl'
      }
    }
  })

  .state('app.scorecards', {
    url: "/scorecards",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/scorecards.html",
        controller: 'AppCtrl'
      }
    }
  })

  .state('app.fnol', {
    url: "/fnol",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/fnol.html",
        controller: 'AppCtrl'
      }
    }
  })

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/login');
})

这是我的登录控制器:

angular.module('starter.controllers', ['highcharts-ng'])

.controller('LoginCtrl', ['$scope','$stateProvider','UserService', function($scope,$stateProvider,UserService) {  

$scope.credentials = {
    username: localStorage.getItem('username') || '',
    password: localStorage.getItem('password') || ''
};

$scope.login = function(credentails) {
    UserService.login(credentails).then(function(user) {
        $scope.loginMessage = false;
        localStorage.setItem('username', $scope.credentials.username);
        localStorage.setItem('password', $scope.credentials.password);
        $state.go('app.studies') ;

    }
     , 
    function(data) {
        $scope.loginMessage = 'Username/Password Invalid';
    }
    );
}

if($scope.credentials.username && $scope.credentials.password){
    $scope.login($scope.credentials);
}

}])

这是我的 UserService,它被注入到登录控制器中:

angular.module('starter.services', [])

.factory('UserService', ['$rootScope', '$q', '$http', function($rootScope, $q, $http) {

    return {

        login: function(credentails) {
            var deffered = $q.defer();

            $http({
                method: 'post',
                url: 'http://localhost/platform/j_spring_security_check',
                params: {
                    'j_username': credentails.username,
                    'j_password': credentails.password
                }
            }).success(function(user, status, headers, config) {
                userLoggedIn = true;
                // $location.path('#/app/studies');
                localStorage.setItem('lastLoginTime', new Date().getTime());
                $rootScope.$broadcast('USER_LOGIN_SUCCESS');
                deffered.resolve(user);
            }).error(function(data, status, headers, config){
                $rootScope.$broadcast('USER_LOGIN_FAILED');
                deffered.reject(data);
            });

            return deffered.promise;
        },


        isUserLoggedIn: function() {
            return userLoggedIn;
        }

    };
}])

当我运行我的应用程序时,我会在控制台中看到:Error: [$injector:unpr] Unknown provider: $stateProviderProvider <- $stateProvider <- LoginCtrl。我知道我的控制器和服务设置正确,因为其他模板及其控制器都可以正常工作。如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

    标签: javascript angularjs dependency-injection angular-ui-router angularjs-service


    【解决方案1】:

    出现错误的原因是,在LoginCtrl 内部,您试图注入$stateProvider 的提供程序,基本上提供程序在控制器内不可用,它们可以作为服务名称访问,如果是@987654323 @ 而不是控制器内部的$stateProvider

    controller('LoginCtrl', ['$scope','$stateProvider','UserService', 
    

    应该是

    controller('LoginCtrl', ['$scope','$state','UserService', 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多