AngularJS如何设置用于显示成功消息和重定向到登录页 AngularJS 在某些时间段

发布时间: 2017/4/16 20:38:16
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我需要注册后显示成功消息 2 分钟,2 分钟后,它应该重定向到登录页。我试着用下面的代码,但它不工作。这里是我的代码。这里是我总更新的代码。

var app = angular.module('accountantApp', []);
app.controller('UserRegistrationController', function($scope, $http, $location,$timeout) {  
$scope.submitRegistrationForm = function(isValid, user) {
    if (isValid) {
        console.log($scope.user);
        $scope.emailError = "";
        $scope.error = "";          
        $http({
          method    : 'POST',
          url       : '../model/signup.php',              
          headers   : {
                        'Content-Type': 'application/json'
                        },
          data      : $scope.user

        }).success(function(data, status, headers) {

            console.log("data is::"+JSON.stringify(data));
            if (data.success != undefined && data.success != '')
            {
                $scope.hideForm = "true";
                $scope.success = data.success;
                $scope.user = "";
                console.log("Success message is:"+data.success);
                $timeout(function() {
        $scope.success = false;
        window.location.href = "login.php";
    }, 1000);
                window.location.href = "login.php";
            } else 
            {                   

                if(data.error == "Please Enter Correct Captcha."){
                    $scope.captchaError =  data.error;
                }else if(data.error == "Email-Id already Exists."){
                    $scope.emailError =  data.error;
                }else{
                    $scope.error = data.error;
                }
                console.log("error message is:"+data.error);
            }


        }).error(function(data, status, headers) {
            $scope.user = '';
            alert("Error occured while creating new account:"+status);
                    });         
    } else{
        $scope.submitted = true;
        alert("Please fill the form.");
        return;
    } 
};
$scope.goToLogin = function(){
    console.log("login called");
    window.location.href = "login.php";
}
});

解决方法 1:

使用 $timeout().then 函数。

   .success(function(data, status, headers) {

        console.log("data is::"+JSON.stringify(data));
        if (data.success != undefined && data.success != '')
        {
            $scope.hideForm = "true";
            $scope.success = data.success;
            $scope.user = "";
            console.log("Success message is:"+data.success);
            $timeout(function() {
                            $scope.success = "going to login page.";
                      }, 10000).then(function(){window.location.href = "login.php";});
            //window.location.href = "login.php"; comment this line.
        } else 
        {                   

            if(data.error == "Please Enter Correct Captcha."){
                $scope.captchaError =  data.error;
            }else if(data.error == "Email-Id already Exists."){
                $scope.emailError =  data.error;
            }else{
                $scope.error = data.error;
            }
            console.log("error message is:"+data.error);
        }


    }).error(function(data, status, headers) {
        $scope.user = '';
        alert("Error occured while creating new account:"+status);
                });         
} else{
    $scope.submitted = true;
    alert("Please fill the form.");
    return;
} 
赞助商