我建立一个角度的应用程序,包括三个(潜在的)最初为用户谁是当前未签署意见:Angular:在视图之间共享控制器需要服务的标志?
intro.html
:赋予用户选项“登录”或“注册”register.html
:新用户注册表单login.html
:现有用户登录表单
我现在有一个服务,auth.service.js
连接到Firebase:
angular
.module('app')
.factory('authService', authService);
authService.$inject = ['$firebaseAuth'];
function authService($firebaseAuth) {
var ref = new Firebase('https://[MY-FIREBASE].firebaseio.com');
return $firebaseAuth(ref);
}
我然后有一个控制器,login.controller.js
,取决于authService
来创建用户帐户,登录用户,连接到Facebook等。这里是控制器的一部分:
angular
.module('app')
.controller('RegisterController', RegisterController);
RegisterController.$inject = ['authService','$location'];
function RegisterController(authService,$location) {
var vm = this;
vm.createUser = function() {
vm.mismatch = false;
if (vm.password === vm.confirm) {
authService.$createUser({
email: vm.email,
password: vm.password
}).then(function(userData) {
$location.path('/people');
}).catch(function(error) {
alert(error);
});
} else {
vm.mismatch = true;
vm.mismatchMessage = 'Password and confirmation must match';
}
};
// login with Facebook
vm.connectFacebook = function() {
authService.$authWithOAuthPopup("facebook").then(function(authData) {
$location.path('/places');
}).catch(function(error) {
alert("Authentication failed:", error);
});
};
...
}
我份额这个控制器在我的三个'intro/register/login'视图之间,但是这对我来说感觉'错误'。会动我的createUser
,connectFacebook
,以及类似的逻辑到我auth.service.js
文件,然后创建每个取决于authService
有更好的方式来处理这一观点“瘦”控制器?