尽管其他控制器和服务工作,未知的提供者错误

Unknown Provider error despite other controllers and services working

本文关键字:未知 提供者 错误 服务工作 其他 控制器      更新时间:2023-09-26

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

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("起动器。控制器",[' 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。我知道我的控制器和服务设置正确,因为其他模板和它们的控制器一起工作。如果有人能帮我解决这个问题,我将非常感激。

你得到错误的原因是,在LoginCtrl内你试图注入的提供者是$stateProvider,基本上提供者是不可用的控制器内,他们是可访问的服务名称,如果是$state而不是$stateProvider在你的控制器

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

应该是

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