【问题标题】:How send error firebase message from service to controller?如何将错误firebase消息从服务发送到控制器?
【发布时间】:2016-03-25 13:15:19
【问题描述】:

我使用 Angular js 和 firebase。 对于我的登录表单和注册表单,我有一个控制器“userController”,它调用我的服务 userProvider 中的函数。

我想在我的视图中显示 firebase 错误消息。我的提供商如何向我的控制器发送消息?

我的代码:

userController.js:

$scope.login = function(user){
            userProvider.login(user);
 }

userProvider.js:

this.login = function(user){
    Auth.$authWithPassword({//on connecte l'utilisateur
      email: user.email,
      password: user.password
    }).then(function(authData) {
        console.log("Utilisateur connecté:", authData.uid);
        $location.path('#/profil');
    }).catch(function(error) {
        console.error("Echec connexion:", error);
        // I want return the error.code for use it in my controller
    });
 }

login.html:

<form ng-hide="authData" class="form-signin col-md-6" name="loginForm" novalidate >
<h2 class="form-signin-heading">Connexion</h2>
<p style="color:red;" ng-show="userDataError">{{userDataErrorMessage}}</p>

<div class="form-group" ng-class="{ 'has-error' : loginForm.email.$invalid }">
    <label for="inputEmail">Email</label>
    <input type="email"  class="form-control" placeholder="Email address" ng-model="user.email" name="email" ng-required autofocus>
    <p class="help-block text-error" ng-show="loginForm.email.$invalid">Cet email n'est pas valide</p>
</div>
<div class="form-group" ng-class="{ 'has-error' : loginForm.password.$invalid }">
    <label for="inputPassword">Password</label>
    <input type="password" class="form-control" placeholder="Password"  ng-model="user.password" ng-minlength="8" name="password" ng-required>
    <p class="help-block text-warning" ng-show="loginForm.password.$error.minlength" >Minimum 8 caractères</p>
</div>
<button  class="btn btn-lg btn-primary btn-block" ng-disabled="loginForm.$invalid" ng-click="login(user)">Connexion</button>

【问题讨论】:

    标签: angularjs firebase angularfire firebase-authentication


    【解决方案1】:

    为了chain函数,返回派生的promise。

    this.login = function(user){
        //return promise
        return derivedPromise = (
            Auth.$authWithPassword({//on connecte l'utilisateur
              email: user.email,
              password: user.password
            }).then(function(authData) {
                console.log("Utilisateur connecté:", authData.uid);
                $location.path('#/profil');
            }).catch(function(error) {
                console.error("Echec connexion:", error);
                //throw error to chain
                throw error;
            });
        );
    });
    

    在控制器中,使用promise来显示错误。

    $scope.login = function(user){
         userProvider.login(user)
             .catch( function onRejected(error) {
                  $scope.errorMessage = ("Echec connexion:" + error);
             });
    };
    

    通过返回承诺,可以使用承诺的.then.catch 方法检索结果(已完成或拒绝)。

    【讨论】:

      【解决方案2】:

      首先对于这些类型的计划,我肯定不会使用提供者,而是使用服务......所以你可以做些什么来解决你向控制器显示错误的问题是:

      在 loginService.js 中

      app.service('loginService', ["$scope", "Auth", "$firebaseAuth", function($scope,Auth,$firebaseAuth){
        var vm= this;
        this.login = function(user){
          Auth.$authWithPassword({//on connecte l'utilisateur
            email: user.email,
            password: user.password
          })
       }
      }])
      

      并在 Ctrl 中:

      app.controller('MainCtrl', function($scope){
      loginService.login().then(function(authData) {
              console.log("Utilisateur connecté:", authData.uid);
              $scope.userData = authData.uid;
              $location.path('#/profil');
          }).catch(function(error) {
              console.error("Echec connexion:", error);
               $scope.errorLogin = error;
              // I want return the error.code for use it in my controller
              $scope.loginError = error;
          });;
      
      })
      

      【讨论】:

      • 当我使用此代码时出现控制台错误:angular.js:13294 TypeError: Cannot read property 'then' of undefined at Scope.$scope.login (userController.js:15) at fn (eval at (angular.js:14138), :4:299) at Scope.$eval (angular.js: 16895) 在 Scope.$apply (angular.js:16995) 在 HTMLButtonElement. (angular.js:24679) 在 defaultHandlerWrapper (angular.js:3417) 在 HTMLButtonElement.eventHandler (angular.js:3405)
      • 好的@cecilecemoi 你知道什么是最好的!如果您可以在plunker 中向我们展示您的代码!
      • 对不起@cecilecemoi 那是我以前的版本。看这里:plnkr.co/edit/JJQDmseLRpypROoaGbLH?p=preview
      • 我没有看到修改
      • 身份验证 $authWithPassword 现在记录.. @cecilecemoi
      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      相关资源
      最近更新 更多