$timeout函数在 if 中的条件后不起作用

$timeout function not working after a condition in if

提问人:Rvdux 提问时间:11/4/2023 最后编辑:ggorlenRvdux 更新时间:11/4/2023 访问量:27

问:

对不起,这是一个愚蠢的问题,我在 AngularJS 上很新,仍在学习它,所以我在这里的问题是 $timeout 函数,它在第一个“window.location.href”语句之后不起作用,我无法弄清楚这个问题。但是,如果我把它放在else语句中,它就可以工作,但这不是我想要的。我希望在执行第一个 URL 时,5 秒后执行第二个 URL。

angular.module('Route').controller('LoginController', ['$scope', 'LoginService', '$timeout',

function ($scope, LoginService, $timeout) {
    $scope.credentials = {
        Login: '',
        Password: ''

    };
 
    $scope.login = function () {
        LoginService.login($scope.credentials)
            .then(function (response) {
                console.log("Response data: ", response.data);
                if (response.data.length > 0) {
                    window.location.href = "http://localhost:8080/Views/home.html#!/employee";

                    $timeout(function () {
                        window.location.href="http://localhost:8080"; }, 5000);
        
                } else {
                    alert("Vérifier vos données s'il vous plâit");
                    $scope.credentials.Password = '';
                }
            })
            .catch(function (error) {
                console.error("Error", error);
            });        
    };
}]);
Javascript AngularJS 的

评论

0赞 ggorlen 11/4/2023
一旦你这样做了,整个页面就会被卸载,所有JS都停止运行,一个新页面会用新的JS加载,所以旧页面的超时被完全杀死了。您可能希望使用 ng-router 或将 nav 保留在 SPA 域中的东西。如果您真的想要加载整个页面,那么您重定向到的页面将需要设置下一个 5 秒计时器。window.location.href = ...
0赞 Pieterjan 11/4/2023
Angularjs 自 2022 年 1 月起停产。你应该改用 Angular。特别是如果你是新手

答: 暂无答案