【问题标题】:AngularJS - Unknown provider: AuthProviderAngularJS - 未知提供者:AuthProvider
【发布时间】:2015-12-17 17:49:47
【问题描述】:

如果用户尝试访问需要登录的页面,我会尝试将用户重定向到登录页面。我正在使用 Firebase 和 AngularJS,遵循this guide。 AngularJS 网站上的error explanation 表示不存在的定义或重复的定义导致了问题,但我无法在我的代码中识别其中任何一个。此外,错误的堆栈跟踪并没有指出我的哪个文件导致了错误,只提到了 angular.js 文件。 谁能告诉我是什么导致了这个问题?

注意:如果我省略了 $routeProvider 的 resolve 部分,该站点可以正常运行,并且用户可以登录和退出。

这是我的 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 = '';
    }
});

【问题讨论】:

  • 您的 index.html 中是否包含 Auth 服务的脚本?
  • 我引用了 angular-route.js 脚本,如果这就是您所指的内容

标签: javascript angularjs firebase firebase-authentication


【解决方案1】:

您的代码指的是一个 Auth 工厂,如 Retrieving Authentication State 下的示例所示。将其包含在您的代码中。

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

【讨论】:

    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多