【问题标题】:$scope value does not udate - Angularfire$scope 值不更新 - Angularfire
【发布时间】:2016-08-02 02:35:05
【问题描述】:

我的代码有问题。 $scope 值在 $createUserWithEmailAndPassword 之后未更新。但如果我发出警报($scope.message),我可以看到警报。我哪里错了?

我正在使用来自 firebase 和 Angularfire 的所有更新文件。

我的 app.js

` var spaapp = angular.module('spaapp', ['ngRoute', 'firebase']);

spaapp.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
    when('/login', {
        templateUrl: 'login.html',
        controller: 'spaController'
    }).
    when('/register', {
        templateUrl: 'register.html',
        controller: 'spaController'
    }).
    when('/success', {
        templateUrl: 'success.html',
        controller: 'spaController'
    }).
    otherwise({
        redirectTo: '/Main'
    });
}]);

' 我的控制器

   spaapp.factory("Auth", ["$firebaseAuth",
  function($firebaseAuth) {
   return $firebaseAuth();
  }
  ]);


spaapp.controller('spaController', ['$scope','$rootScope', 'Auth',function ($scope,$rootScope,Auth) {
   // $scope.authObj=firebaseAuth();
   //var auth = Auth;
    $scope.login = function () {

    }

    $scope.register = function () {


       var email = $scope.user.email;
      var password = $scope.user.password;

    Auth.$createUserWithEmailAndPassword(email, password).then(function(regUser){
            $scope.message="Hi" + regUser.uid;
            console.log("Signed in as:" + regUser.uid );


        }).catch(
    function(error) {  
     var errorCode = error.code;
     var errorMessage = error.message;
     console.log(error.message);
     $scope.message = error.message;
     });
    };


}]);

感谢任何帮助。

谢谢

【问题讨论】:

    标签: angularjs firebase angularfire firebase-authentication


    【解决方案1】:

    你可以通过在$scope上调用$apply()来刷新angular的$scope

    例子:

    spaapp.factory("Auth", ["$firebaseAuth",
      function($firebaseAuth) {
       return $firebaseAuth();
      }
      ]);
    
    
    spaapp.controller('spaController', ['$scope','$rootScope', 'Auth',function ($scope,$rootScope,Auth) {
       // $scope.authObj=firebaseAuth();
       //var auth = Auth;
        $scope.login = function () {
    
        }
    
        $scope.register = function () {
    
    
           var email = $scope.user.email;
          var password = $scope.user.password;
    
        Auth.$createUserWithEmailAndPassword(email, password).then(function(regUser){
                $scope.message="Hi" + regUser.uid;
                console.log("Signed in as:" + regUser.uid );
    
    
            }).catch(
        function(error) {  
         var errorCode = error.code;
         var errorMessage = error.message;
         console.log(error.message);
         $scope.message = error.message;
    
         $scope.$apply() // HERE
    
         });
        };
    
    
    }]);
    

    有关 Angular 的摘要循环的更多信息,请访问:
    http://jimhoskins.com/2012/12/17/angularjs-and-apply.html

    【讨论】:

    • 谢谢,我之前使用过 '$scope.$apply()' 但它给了我一个错误 Error: [$rootScope:inprog] .
    猜你喜欢
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多