【发布时间】:2018-04-03 22:22:31
【问题描述】:
我遇到了一个无法解决的 $scope 问题。
我的应用程序使用 Google Firebase API 通过发布到 API 来验证用户身份,然后通过更新 $scope 变量向用户显示成功/错误消息。这是我的功能
$scope.message = "";
$scope.signin = function () {
var auth = firebase.auth();
auth.signInWithEmailAndPassword(signinEmail, signinPassword).catch(function (error) {
var errorCode = error.code;
console.log("errorCode " + errorCode);
$scope.message = "An error Alert";
});
}
在我的登录函数中更新 $scope 工作正常,但在内部 catch 函数中访问它不起作用。
到目前为止我已经尝试过:
将我的代码更改为使用“this”而不是 $scope - 这似乎会引起更大的头痛,因为使用“this”时 html 显示错误。 (可能是由于该应用程序使用 ng-Route)我想保留我的应用程序使用 $scope 或 'this'。
我尝试以各种方式将 $scope 注入到函数中,但均未成功。
我试图创建第二个函数,可以从我的 catch 块中调用,
$scope.updateMessage(){ },但由于它位于 $scope 上,因此不会调用该函数。如果将此用作常规函数var fn = updateMessage(){ },则无法访问范围。
【问题讨论】:
-
盲猜,尝试将
catch函数附加到当前上下文:.catch(...).bind(this)? -
您正在更新 Angular 上下文之外的范围变量,需要告诉 Angular 运行摘要。建议您使用
angularfire模块来简化此操作 -
试试 $scope.$apply(function(){$scope.message = "An error Alert";})
标签: javascript angularjs firebase angularjs-scope firebase-authentication