AngularJS-未知提供程序:AuthProvider

AngularJS - Unknown provider: AuthProvider

本文关键字:AuthProvider 程序 未知 AngularJS-      更新时间:2023-09-26

如果用户试图访问需要登录的页面,我会尝试将他们重定向到登录页面。我使用的是Firebase和AngularJS,遵循本指南。AngularJS网站上的错误解释表明,不存在的定义或重复的定义导致了问题,但我无法在代码中识别这两种情况。此外,错误的堆栈跟踪并没有指出是我的哪个文件导致了错误,只提到了angular.js文件。有人能告诉我是什么导致了这个问题吗?

注意:如果我省略$routeProvider的解析部分,该网站运行时不会出现错误,用户可以登录和注销。

这是我的app.js

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'
    });
});

这是main.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();
    }
});

这是thread.js控制器

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 = '';
    }
});

您的代码引用的是一个Auth工厂,该工厂显示在检索身份验证状态下的示例中。将其包含在代码中。

.factory("Auth", ["$firebaseAuth",
  function($firebaseAuth) {
    var ref = new Firebase("<YOUR FIREBASE>");
    return $firebaseAuth(ref);
  }
]);