【发布时间】:2015-01-22 15:03:35
【问题描述】:
我正在尝试存储一个授权用户 id 变量,我可以将其传递给控制器。我知道我尝试从工厂对象的闭包内部传递数据的方式存在问题,但我一直不知道如何解决它。
这是我的工厂:
myApp.factory('Authentication', function($firebase,
$firebaseAuth, FIREBASE_URL, $location) {
var ref = new Firebase(FIREBASE_URL);
var simpleLogin = $firebaseAuth(ref);
var authorized;
var myObject = {
login : function() {
return simpleLogin.$authAnonymously().then(function(authData) {
authorized = authData.uid;
console.log("Logged in as:", authData.uid);
}).catch(function(error) {
console.error("Authentication failed:", error);
});
},
auth : authorized
} //myObject
return myObject;
});
这是我的控制器:
myApp.controller('MeetingsController',
function($scope, $firebase, Authentication) {
var ref = new Firebase('http://i2b2icons.firebaseio.com/');
var meetings = $firebase(ref);
$scope.authid = Authentication.auth;
$scope.meetings = meetings.$asObject();
// $scope.id = = Authentication.login.id;
$scope.addMeeting=function() {
meetings.$push({
name: $scope.meetingname,
date: Firebase.ServerValue.TIMESTAMP
}).then(function() {
$scope.meetingname = '';
});
} //addmeeting
$scope.deleteMeeting=function(key) {
meetings.$remove(key);
} //deletemeeting
}); //MeetingsController
我真的只是想从 myObject 的登录函数中获取 $scope.authid 变量来获取 auauthorized 的值。
应该已经通过这个控制器登录调用了登录方法:
myApp.controller('RegistrationController',
function($scope, $firebaseAuth, $location, Authentication) {
$scope.login = function() {
Authentication.login();
} //login
}); //RegistrationController
【问题讨论】:
-
Authentication.auth` 仅设置在
Authentication.login内部,这是您没有调用的函数。