Angularjs 从工厂到控制器的角度传递数据
我试图存储一个授权用户id变量,我可以将其传递给控制器。我知道我试图从工厂对象的闭包内部传递数据时遇到了问题,但我一直在研究如何解决这个问题 这是我的工厂:Angularjs 从工厂到控制器的角度传递数据,angularjs,firebase,Angularjs,Firebase,我试图存储一个授权用户id变量,我可以将其传递给控制器。我知道我试图从工厂对象的闭包内部传递数据时遇到了问题,但我一直在研究如何解决这个问题 这是我的工厂: myApp.factory('Authentication', function($firebase, $firebaseAuth, FIREBASE_URL, $location) { var ref = new Firebase(FIREBASE_URL); var simpleLogin = $firebaseAuth
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
myApp.controller('RegistrationController',
function($scope, $firebaseAuth, $location, Authentication) {
$scope.login = function() {
Authentication.login();
} //login
}); //RegistrationController
我只是想让$scope.authid变量从myObject的登录函数中获取AUAuthorized的值
应已通过此控制器登录调用登录方法:
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
myApp.controller('RegistrationController',
function($scope, $firebaseAuth, $location, Authentication) {
$scope.login = function() {
Authentication.login();
} //login
}); //RegistrationController
您只是在工厂中设置局部变量
authorized
,它与您试图在控制器中访问的身份验证.auth
无关(当然,除非您在创建因子时将值设置为它,而这无论如何都不是目的)。而是在工厂中返回预定义对象并从中返回该对象。设置对象引用的属性
myApp.factory('Authentication', function($firebase,
$firebaseAuth, FIREBASE_URL, $location) {
var ref = new Firebase(FIREBASE_URL);
var simpleLogin = $firebaseAuth(ref);
//Predefine the factory
var factory = {
login: login,
authorized: null
};
function login() {
return simpleLogin.$authAnonymously().then(function(authData) {
factory.authorized = authData.uid; //Set the property here
}).catch(function(error) {});
}
//return it
return factory;
});
如果您具有工厂的引用,并且对其属性的更新将反映(如果您调用填充数据的方法)在控制器中。另一种方法是在工厂中使用getter函数返回auth对象,或者您也可以缓存login函数返回的承诺,并在发生注销用户的事件时将其返回并使其无效。正如其他人已经指出的,您只更新变量
authorized
,不是属性auth
。一个相当简单的解决方案是将auth
更改为始终返回当前值的getter:
var myObject = {
login : function() {
...
},
get auth() {
return authorized;
}
您不必更改任何其他代码。身份验证。auth`仅在
身份验证中设置。登录
,您不需要调用此函数。谢谢!这很有道理,并且澄清了很多事情。