【问题标题】:Why do i need to refresh page to view new value of ng-show为什么我需要刷新页面才能查看 ng-show 的新值
【发布时间】:2014-12-02 16:29:54
【问题描述】:

大家好,我正在使用 firebase 和 Angular 来构建示例应用程序。

这是注册函数

$scope.register = function (){ 
    Authentication.register($scope.user)
        .then(function(user){
            Authentication.login($scope.user);                                                      
        })
        .then(function(user){
            $timeout(function(){
                $location.path('/meetings');
            }, 5);
        })
        .catch(function(error){
            $scope.message = error.toString();
        });


} //register

此函数调用此工厂中的方法

myApp.factory('Authentication', function($firebase,
                $firebaseAuth,$location, FIREBASE_URL){

    var ref = new Firebase (FIREBASE_URL);
    var auth = $firebaseAuth(ref);      

    var myObject = {
        login : function (user){
                return auth.$authWithPassword({
                    email: user.email,
                    password: user.password
                });
        }, // login

        logout: function (){
            return auth.$unauth();
        },
        register : function (user){
            return auth.$createUser(user.email,user.password);
        } //register
    } //myObject
    return myObject;
});

这里是 status.js 控制器,它根据用户是否登录来更改我的索引

auth.$onAuth(function(authData){
        if (authData){
            console.log("i entered authData");
            $scope.userStatus = authData.password.email;

        } else {

            $scope.userStatus = false;
        }
    });

index.html 文件的一部分

<div class="userinfo" 
ng-controller="StatusController"  ng-show="userStatus">
  <span class="user">Hi {{userStatus}}</span>
  <a href="#" ng-click="logout()">Log Out</a>
 </div>

我的问题是 ng-view 需要刷新页面才能显示新值。它没有自动显示,但我的代码有效。如果我手动刷新页面,我可以看到用户已注册并登录。

现在搜索大约 2 小时,$scope.$apply here 似乎不是这样。

提前谢谢你

【问题讨论】:

  • 不知道答案,但是文档中提到的是onAuth而不是$onAuth,会不会是这样?
  • 尽量让 ng-show 不与控制器内联。
  • 为什么奇怪的myObject 包装器什么都不做?考虑只使用return $firebaseAuth(ref);

标签: angularjs firebase registration ng-show


【解决方案1】:

我想感谢你们提供可能的解决方案,但似乎解决方案在于 $timeout 变量。

因为下面的注册函数与 API 对话,而我正在使用 GulpJS 在本地环境中工作,所以我的脚本和 Firebase API 之间的对话存在延迟。

$scope.register = function (){ 
Authentication.register($scope.user)
    .then(function(user){
        Authentication.login($scope.user);                                                      
    })
    .then(function(user){
        $timeout(function(){
            $location.path('/meetings');
        }, 5);
    })
    .catch(function(error){
        $scope.message = error.toString();
    });


} //register

这就是为什么我首先在​​其中放了一个 $timeout 角度函数,但我忘记了 $timeout 函数中输入的数字 5 以毫秒为单位,这似乎是因为我的设置(在与Firebase API 在线)我发现如果我在重定向中使用 2 秒延迟,现在 ng-show 会自动更改,而无需手动刷新页面。

正确的代码如下:

$scope.register = function (){ 
    Authentication.register($scope.user)
        .then(function(user){
            Authentication.login($scope.user);                                                      
        })
        .then(function(user){
            $timeout(function(){
                $location.path('/meetings');
            }, 2000);
        })
        .catch(function(error){
            $scope.message = error.toString();
        });
} //register

我怀疑如果我在网络上使用我的 Angular 应用程序,我可能会降低延迟甚至完全删除 $timeout 功能。

【讨论】:

    猜你喜欢
    • 2014-07-16
    • 2013-12-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    相关资源
    最近更新 更多