Angularjs 如何将错误firebase消息从服务发送到控制器?

Angularjs 如何将错误firebase消息从服务发送到控制器?,angularjs,firebase,angularfire,firebase-authentication,Angularjs,Firebase,Angularfire,Firebase Authentication,我使用Angular js和firebase。 对于我的登录表单和注册表单,我有一个控制器“userController”,它调用我的服务userProvider中的函数 我想在我的视图中显示firebase错误消息。我的提供商如何向我的控制器发送消息 我的代码: userController.js: $scope.login=函数(用户){ userProvider.login(用户); } userProvider.js: this.login=函数(用户){ Auth.$authWith

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

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

我的代码:

userController.js:

$scope.login=函数(用户){
userProvider.login(用户);
}
userProvider.js:

this.login=函数(用户){
Auth.$authWithPassword({//on connecte l'usilisateur
电子邮件:user.email,
密码:user.password
}).then(函数(authData){
日志(“利用连接:”,authData.uid);
$location.path(“#/profile”);
}).catch(函数(错误){
控制台错误(“Echec连接:,错误);
//我想返回error.code以便在我的控制器中使用它
});
}
login.html:


连接

{{{userDataErrorMessage}

电子邮件

Cet电子邮件无效

密码

最小8克拉

连接

首先,对于这类计划,我肯定不会使用提供商,而是使用服务。。。因此,要解决向控制器显示错误的问题,您可以做的是:

在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;
    });;

})

首先,对于这些类型的计划,我肯定不会使用提供商,而是使用服务。。。因此,要解决向控制器显示错误的问题,您可以做的是:

在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;
    });;

})

要从函数中链接返回导出的承诺

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;
        });
    );
});
在控制器中,使用承诺来显示错误

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

通过返回承诺,可以使用
检索结果(完成或拒绝)。然后
捕获
承诺方法。

,从函数中返回衍生承诺

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;
        });
    );
});
在控制器中,使用承诺来显示错误

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

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

当我使用此代码时,我有一个控制台错误:angular.js:13294 TypeError:无法读取作用域未定义的属性“then”。$Scope.login(userController.js:15)在fn(eval at(angular.js:14138),:4:299)在expensiveCheckFn(angular.js:15151)在回调(angular.js:24674)在作用域。$eval(angular.js:16895)在作用域。$apply(angular.js:16995)在HTMLButtonElement。(angular.js:24679)在defaultHandlerWrapper(angular.js:3417)在HtmlButtoneElement.eventHandler(angular.js:3405)Ok@cecilecemoi你知道什么是最好的!如果你能在一个窗口中显示你的代码!抱歉@cecilecemoi那是我以前的版本。。看这里:我没有看到修改身份验证$authWithPassword now日志@CecileCeMoi当我使用这段代码时,我有一个控制台错误:angular.js:13294 TypeError:无法读取作用域中未定义的属性'then'。$Scope.login(userController.js:15)在fn(eval at(angular.js:14138),:4:299)在expensiveCheckFn(angular.js:15151)在回调(angular.js:24674)在作用域中。$eval(angular.js:16895)在范围中。在HTMLButtoneElement中应用$apply(angular.js:16995)。(angular.js:24679)在defaultHandlerWrapper(angular.js:3417)在HtmlButtoneElement.eventHandler(angular.js:3405)Ok@cecilecemoi你知道什么是最好的!如果你能在一个窗口中显示你的代码!抱歉@cecilecemoi那是我以前的版本。。看这里:我没有看到修改身份验证$authWithPassword now日志@塞西莱切莫