我试图将我的Firebase身份验证逻辑从控制器移至工厂,但遇到基本设置问题。我正在使用Angularfire在我的应用中完成身份验证,并遵循文档here。如何正确设置工厂
在我的控制器中一切正常,但我不知道如何正确设置代码在工厂。我当前的代码如下:
angular
.module('app')
.factory('authService', authService);
authService.$inject = ['$firebaseAuth'];
var ref = new Firebase('https://[MY-FIREBASE].firebaseio.com');
var auth = $firebaseAuth(ref);
return {
createUser(email,password): auth.$createUser({
email: email,
password: password
}).then(function(userData) {
$location.path('/people');
}).catch(function(error) {
alert(error);
}),
connectFacebook: auth.$authWithOAuthPopup("facebook").then(function(authData) {
$location.path('/places');
}).catch(function(error) {
alert("Authentication failed:", error);
}),
removeUser: auth.$removeUser({
email: vm.email,
password: vm.password
}).then(function() {
alert('User removed');
}).catch(function(error) {
alert(error);
})
}
是我的目标做的是注入这家工厂为我登录控制器并调用相应的功能线了点击事件,如authService.createUser($scope.email,$scope.password);
。
谢谢!直到这个解释之前,我实在没有完全理解服务的用处。许多课程和教程通过掩盖服务和“在几乎所有情况下工厂将是最佳选择”来破坏服务。出于这个原因,我一直在寻找让工厂满足我的需求,而不是现在! – MattDionis
角度教程的景观可能会令人困惑。我仍然没有发现一个我认为在用例之间有明确区别的例子,但是我提出了一套自己的规则,它们运行得很好。 –