Angularjs $routeProvider 解析返回的未知供应商
Angularjs $routeProvider resolve returning unknown provider
我正在尝试对用户的登录进行身份验证,并防止他们在未登录时访问某些路由。我有以下代码:
var isUserLoggedIn = function($q, $timeout, $http, $Location, $rootScope) {
// Initialize a new promise.
var deferred = $q.defer();
// Check is user is logged in
$http.get('/isLoggedIn').success(function(user) {
if(user !== '0') { // User is logged in
deferred.resolve();
} else { // Use is not logged in
$rootScope.message = 'You need to log in.';
deferred.reject();
$location.url('/login');
}
});
return deferred.promise;
};
$routeProvider
.when('/', {
templateUrl: 'partials/index',
controller: 'IndexCtrl'
})
.when('/registerUser', {
templateUrl: 'partials/registerUser',
controller: 'RegisterUserCtrl'
})
.when('/login', {
templateUrl: 'partials/login',
controller: 'LoginCtrl'
})
.when('/userProfile', {
templateUrl: 'partials/userProfile',
controller: 'UserProfileCtrl',
resolve: {
loggedin: isUserLoggedIn
}
})
.otherwise({
redirectTo: '/'
});
对于控制器,我有以下内容:
angular.module('enigmaApp.controllers', [])
.controller('UserProfileCtrl', ['$scope', 'loggedin', function($scope, loggedin) {
$scope.name = "john doe";
$scope.loggedin = loggedin;
}]);
但是,当我尝试访问 /userProfile 路由时出现以下错误。
错误:[$injector:unpr] 未知提供者:$LocationProvider <- $Location <- loggedin
在这里搜索了一堆答案后,大多数人的问题似乎是在他们的观点中使用 ng-controller,但我没有使用它。我的翡翠观点如下:
Layout.jade
<!DOCTYPE html>
html(ng-app="enigmaApp", lang="en")
head
meta(charset='utf8')
meta(name='viewport', content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no')
base(href='/')
title Enigma base platform
link(rel='stylesheet', href='/css/app.css')
link(rel="stylesheet", href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css')
body
div.content-wrapper
block body
index.jade
extends layout
block body
div.user.f-right(ng-controller='NavCtrl')
a(href="/") Home
| |
a(href='/login') Login
| |
a(href='registerUser') Register
| |
a(href='/logout', target='_self') Logout
| |
a(href='/userProfile') User Profile
h1 Enigma Health
div(ng-view)
userProfile.jade
h3 Welcome {{name}}!
正如我在评论中提到的,将 $Location
更改为 $location
解决了这个问题。
我正在尝试对用户的登录进行身份验证,并防止他们在未登录时访问某些路由。我有以下代码:
var isUserLoggedIn = function($q, $timeout, $http, $Location, $rootScope) {
// Initialize a new promise.
var deferred = $q.defer();
// Check is user is logged in
$http.get('/isLoggedIn').success(function(user) {
if(user !== '0') { // User is logged in
deferred.resolve();
} else { // Use is not logged in
$rootScope.message = 'You need to log in.';
deferred.reject();
$location.url('/login');
}
});
return deferred.promise;
};
$routeProvider
.when('/', {
templateUrl: 'partials/index',
controller: 'IndexCtrl'
})
.when('/registerUser', {
templateUrl: 'partials/registerUser',
controller: 'RegisterUserCtrl'
})
.when('/login', {
templateUrl: 'partials/login',
controller: 'LoginCtrl'
})
.when('/userProfile', {
templateUrl: 'partials/userProfile',
controller: 'UserProfileCtrl',
resolve: {
loggedin: isUserLoggedIn
}
})
.otherwise({
redirectTo: '/'
});
对于控制器,我有以下内容:
angular.module('enigmaApp.controllers', [])
.controller('UserProfileCtrl', ['$scope', 'loggedin', function($scope, loggedin) {
$scope.name = "john doe";
$scope.loggedin = loggedin;
}]);
但是,当我尝试访问 /userProfile 路由时出现以下错误。
错误:[$injector:unpr] 未知提供者:$LocationProvider <- $Location <- loggedin
在这里搜索了一堆答案后,大多数人的问题似乎是在他们的观点中使用 ng-controller,但我没有使用它。我的翡翠观点如下:
Layout.jade
<!DOCTYPE html>
html(ng-app="enigmaApp", lang="en")
head
meta(charset='utf8')
meta(name='viewport', content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no')
base(href='/')
title Enigma base platform
link(rel='stylesheet', href='/css/app.css')
link(rel="stylesheet", href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css')
body
div.content-wrapper
block body
index.jade
extends layout
block body
div.user.f-right(ng-controller='NavCtrl')
a(href="/") Home
| |
a(href='/login') Login
| |
a(href='registerUser') Register
| |
a(href='/logout', target='_self') Logout
| |
a(href='/userProfile') User Profile
h1 Enigma Health
div(ng-view)
userProfile.jade
h3 Welcome {{name}}!
正如我在评论中提到的,将 $Location
更改为 $location
解决了这个问题。