AngularJS中的奇怪重定向

时间:2017-03-07 20:50:55

标签: javascript angularjs firebase firebase-authentication

我目前正在使用Firebase日志记录系统开发使用AngularJS制作的应用。当我尝试登录时,我有一个奇怪的重定向 - 无论日志记录是否成功,它都会出于某种原因重定向到注册页面。如果警报显示出来,它会显示在注册页面上。 试图删除注册页面的路线 - 没有运气。尝试删除OnAuthStateChanged触发时发生的操作 - 仍然重定向。尝试调试应用程序 - 似乎功能

for (var i = 0; i < eventFnsLength; i++) {
  if (!event.isImmediatePropagationStopped()) {
    handlerWrapper(element, event, eventFns[i]);
  }
}
第3532行的angular.js中的

导致了这一点,但我不知道如何让它停止。

SignInCtrl.js

angular.module('szybkiePisanie')
.controller('SignInCtrl', ["$scope", "$firebaseAuth", function ($scope, $firebaseAuth) {
    $scope.SignIn = function (event) {
        if ($scope.user.email != undefined) var email = $scope.user.email; else { alert("Wpisz adres e-mail"); return; }
        if ($scope.user.password != undefined) var password = $scope.user.password; else { alert("Wpisz hasło"); return; }
        firebase.auth().signInWithEmailAndPassword(email, password).catch(function (error) {                
            var errorCode = error.code;
            var errorMessage = error.message;

            if (email.length < 4) {
                alert('Proszę wpisać adres email.');
                return;
            }
            if (password.length < 4) {
                alert('Proszę wpisać hasło.');
                return;
            }
            ;
            if (errorCode === 'auth/wrong-password') {
                alert('Błędne hasło.');
            }
            else if (errorCode === 'auth/invalid-email') {
                alert('Nieprawidłowy adres email');
            }
            else if (errorCode === 'auth/user-disabled') {
                alert('Użytkownik zablokowany');
            }
            else if (errorCode) {
                alert(errorMessage);
            }
        })
    }
}]);

RegisterCtrl.js

angular.module('szybkiePisanie')
.controller('RegisterCtrl', ["$scope", "$firebaseAuth", function ($scope, $firebaseAuth) {
    $scope.Register = function (event) {
        if ($scope.user.email != undefined) var email = $scope.user.email; else { alert("Wpisz mejl"); return; }
        if ($scope.user.password != undefined) var password = $scope.user.password; else { alert("Wpisz haslo"); return; }
        firebase.auth().createUserWithEmailAndPassword(email, password).catch(function (error) {
            var errorCode = error.code;
            var errorMessage = error.message;

            if (email.length < 4) {
                alert('Please enter an email address.');
                return;
            }
            if (password.length < 4) {
                alert('Please enter a password.');
                return;
            }
        })
    }
}]);

的script.js(路由)

angular.module('szybkiePisanie', ['ngRoute', 'firebase'])
.config(['$routeProvider',
    function ($routeProvider) {
        $routeProvider
            .when('/home', {
                templateUrl: 'Home/home.html',
                controller: 'HomeCtrl'
            })

            .when('/practice', {
                templateUrl: 'Practice/practice.html',
                controller: 'PracticeCtrl'
            })

            .when('/signin', {
                templateUrl: 'SignIn/signin.html',
                controller: 'SignInCtrl'
            })

            .when('/profile', {
                templateUrl: 'Profile/profile.html',
                controller: 'ProfileCtrl'
            })

            .when('/register', {
                templateUrl: 'Register/register.html',
                controller: 'RegisterCtrl'
            })

            .otherwise({
                redirectTo: '/home'
            })
    }]);

编辑: 重定向不仅仅是由Singing in引起的。事实上,任何按钮或下拉菜单都会导致我的应用程序从'/ singin'重定向到'/ register'。奇怪。

2 个答案:

答案 0 :(得分:2)

所以我想出了导致重定向的原因。我还没有结束这个&#39; a&#39;在我的网站上标记,网站的其余部分充当链接。

答案 1 :(得分:0)

您只指定了两条可能的路线:/signin/register。因此,路由器将只支持其中一条路由,您最终将被重定向到其中一条路由。问题可能是您没有为登录状态定义路由,该状态应该是默认路由的一种。据我所知,在使用角度路由器时,您需要定义您想要支持的每条路线。

    angular.module('szybkiePisanie', ['ngRoute', 'firebase'])
    .config(['$routeProvider',
        function ($routeProvider) {
            $routeProvider
                .when('/signin', {
                    templateUrl: 'SignIn/signin.html',
                    controller: 'SignInCtrl'
                })

                .when('/register', {
                    templateUrl: 'Register/register.html',
                    controller: 'RegisterCtrl'
                })

                .when('/', {
                    templateUrl: 'Home/home.html',
                    controller: 'HomeCtrl'
                })

                .otherwise({
                    redirectTo: '/'
                });
        }]);