【问题标题】:angularjs: ng-show updating before window.location.replace changes templateangularjs:ng-show 在 window.location.replace 更改模板之前更新
【发布时间】:2018-03-07 12:40:49
【问题描述】:

我正在尝试在显示登录页面时使用 ng-show 隐藏页面顶部的菜单。用户登录后,我在调用 window.location.replace("/") 后更新绑定到 ng-show 的变量以显示菜单,这会将用户重定向到他的首页。

问题是绑定到 ng-show 的作用域变量在 window.location.replace("/") 之后就在同一个函数中,使菜单在重定向完成之前可见,这会导致恼人的口吃登录表单。

有没有办法让范围变量只在重定向完成后才更新?

这是我的登录功能供参考:

    $scope.login = function() {
            $scope.loginError = null;
            $scope.loginLoader = true;
            $http.post('/authenticate', $scope.post)
              .then(function(res) {
                    if(res.data.success == true){
                            $scope.post = {};

                            $scope.storage.user = {};
                            $scope.storage.user.oikeudet = {};
                            $scope.storage.user.siteConf = {};
                            $scope.storage.user.ryhmät = [];

                            $scope.storage.user.accessToken = res.data.token;
                            $scope.storage.user.name = res.data.user;

                            for(oikeus in res.data.ryhmäoikeudet){
                                    $scope.storage.user.ryhmät.push(oikeus);
                                    for(id in res.data.ryhmäoikeudet[oikeus]){
                                            if(!(id in $scope.storage.user.oikeudet)){
                                                    $scope.storage.user.oikeudet[id] = res.data.ryhmäoikeudet[oikeus][id];
                                            }
                                    }
                            }
                            for(asetus in res.data.conf){
                                    console.log(res.data.conf[asetus].asetus);
                                    if(res.data.conf[asetus].asetus == 1){
                                            res.data.conf[asetus].asetus = true;
                                    }else if (res.data.conf[asetus].asetus == 0){
                                            res.data.conf[asetus].asetus = false;
                                    }
                                    $scope.storage.user.siteConf[res.data.conf[asetus].nimi] = res.data.conf[asetus].asetus;

                            }

                            window.location.replace("/");
                            $scope.loginmenu = '#logout';
                            $scope.loggedin = true;
                            $scope.user = $scope.storage.user;
                    }else{
                            $scope.loginerror = res.data.message;
                    }
                    $scope.loginLoader = false;
            }
         ,function(error) {
                    $scope.loginError = "Kirjautuminen epäonnistui";
                    $scope.loginLoader = false;
                    console.log('Error: ' + data);
        });
    };

【问题讨论】:

    标签: javascript angularjs scope url-redirection


    【解决方案1】:

    您可以使用$timeout 将范围变量更改移动到单独的线程。这样,第一个摘要循环将在执行范围变量更改之前完成:

    window.location.replace("/");
    $timeout(function() {
      $scope.loginmenu = '#logout';
      $scope.loggedin = true;
      $scope.user = $scope.storage.user;
    });
    

    您不需要设置延迟,因为将其移至单独的线程就足够了。有关$timeout 的更多信息:https://docs.angularjs.org/api/ng/service/$timeout

    【讨论】:

    • 我用你的建议让它工作了,谢谢!出于某种原因,尽管我不得不设置 1 毫秒的超时时间,否则它将无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多