Javascript AngularJS-未知提供程序:AuthProvider
如果用户试图访问需要登录的页面,我会尝试将用户重定向到登录页面。我正在使用Firebase和AngularJS,如下所示。AngularJS站点上的显示要么是不存在的定义,要么是重复的定义导致了问题,但我无法在代码中识别这两个。此外,错误的堆栈跟踪没有指出是哪个文件导致了错误,只提到了angular.js文件。 谁能告诉我是什么导致了这个问题 注意:如果我省略$routeProvider的resolve部分,站点运行时不会出现错误,用户可以登录和注销 这是我的app.jsJavascript AngularJS-未知提供程序:AuthProvider,javascript,angularjs,firebase,firebase-authentication,Javascript,Angularjs,Firebase,Firebase Authentication,如果用户试图访问需要登录的页面,我会尝试将用户重定向到登录页面。我正在使用Firebase和AngularJS,如下所示。AngularJS站点上的显示要么是不存在的定义,要么是重复的定义导致了问题,但我无法在代码中识别这两个。此外,错误的堆栈跟踪没有指出是哪个文件导致了错误,只提到了angular.js文件。 谁能告诉我是什么导致了这个问题 注意:如果我省略$routeProvider的resolve部分,站点运行时不会出现错误,用户可以登录和注销 这是我的app.js angular.mod
angular.module('richWebApp', ['ngRoute', 'firebase', 'objectFilter'])
.constant('fb', {
url: 'https://<my-firebase-app>.firebaseio.com/' //name removed for security reasons
})
.run(function($rootScope, $location) {
$rootScope.$on("$routeChangeError", function(event, next, previous, error) {
if(error === "AUTH_REQUIRED") {
$location.path("/login");
}
});
})
.config(function($routeProvider){
$routeProvider.
when('/login', {
templateUrl: 'pages/login/login.html'
}).
when('/main', {
templateUrl: 'pages/main/main.html',
resolve: {
"currentAuth": ["Auth", function(Auth) {
return Auth.$requireAuth();
}]
}
}).
when('/thread/:threadId', {
templateUrl: 'pages/thread/thread.html',
resolve: {
"currentAuth": ["Auth", function(Auth) {
return Auth.$requireAuth();
}]
}
}).
otherwise({
redirectTo: '/login'
});
});
这是thread.js控制器
angular.module('richWebApp')
.controller('mainPageController', function($scope, $location, userService, currentAuth, threadService, fb, $firebaseAuth, $filter){
$scope.user = userService.getLoggedInUser();
$scope.newThreadTitle = '';
$scope.threadSubject = ''
$scope.createNewThread = false;
$scope.sortBy = 'dateAdded'
$scope.threads = threadService.getAllThreads();
$scope.getSubjects = function(subject) {
return $scope.threads.subject;
}
$scope.beginAddThread = function() {
$scope.createNewThread = true;
}
$scope.addThread = function(){
if(!$scope.newThreadTitle || !$scope.newThreadSubject){
return false;
}
var date = new Date();
var newThread = {
title: $scope.newThreadTitle,
subject: $scope.newThreadSubject,
username: $scope.user.name,
numComments: 0,
comments: [],
dateAdded: date.getTime()
};
$scope.threads.$add(newThread);
$scope.newThread = '';
$scope.newThreadTitle = '';
$scope.newThreadSubject = '';
$scope.createNewThread = false;
}
$scope.sortByDate = function() {
$scope.sortBy = 'dateAdded';
}
$scope.sortByPopularity = function() {
$scope.sortBy = 'numComments';
}
$scope.searchSubject = function(subject) {
$scope.searchThread = subject;
}
$scope.logout = function(){
userService.logout();
}
});
angular.module('richWebApp')
.controller('threadPageController', function($scope, $location, $routeParams, $filter, currentAuth, threadService, fb, userService){
var threadId = $routeParams.threadId;
$scope.newComment = '';
var thread = threadService.getThread(threadId);
thread.$bindTo($scope, 'thread')
$scope.addComment= function(){
if(!$scope.newComment){
return false;
}
var currentUser = userService.getLoggedInUser();
var date = new Date();
var newComment = {
text: $scope.newComment,
username: currentUser.name,
dateAdded: date.getTime(),
userPic: currentUser.profilePic
};
$scope.thread.comments = $scope.thread.comments || [];
$scope.thread.comments.push(newComment);
$scope.thread.numComments += 1;
$scope.newComment = '';
}
});
您的代码引用的是身份验证工厂,如下面的示例所示。在代码中包含这一点
.factory("Auth", ["$firebaseAuth",
function($firebaseAuth) {
var ref = new Firebase("<YOUR FIREBASE>");
return $firebaseAuth(ref);
}
]);
.factory(“Auth”,[“$firebaseAuth”,
函数($firebaseAuth){
var ref=新的Firebase(“”);
返回$firebaseAuth(ref);
}
]);
您是否在index.html中包含了Auth服务的脚本?如果您指的是angular-route.js脚本,我会引用它