当我登录时触发,当我登录时,但在页面刷新时它不再触发?我应该如何设置一个触发的函数?
似乎当我刷新页面时,它会重新连接并重新验证(参考下面的firebase日志),但是(我的猜测是)没有触发
0: event:/users:{"ep":"simplelogin:145","sp":"simplelogin:145"}:value
给了什么?
谢谢瑞安
控制器:
'use strict';
angular.module('ccApp.services.user', ['ccApp.services.auth'])
.factory('User', ['$firebase', '$rootScope', 'FBURL',
function($firebase, $rootScope, FBURL){
var ref = new Firebase(FBURL + '/users');
var users = $firebase(ref);
var User = {
create: function (authUser, name) {
/* jshint camelcase: false */
console.log(authUser.id);
console.log(authUser.user);
users[authUser.id] = {
md5_hash: authUser.md5_hash,
name: name,
$priority: authUser.uid
};
users.$save(authUser.id).then(function () {
console.log(authUser.id);
setCurrentUser(authUser.id);
});
},
findByUserId: function (userId) {
if (userId) {
return users.$child(userId);
}
},
getCurrent: function () {
return $rootScope.currentUser;
},
signedIn: function () {
return $rootScope.currentUser !== undefined;
}
};
function setCurrentUser (userId) {
$rootScope.currentUser = User.findByUserId(userId);
}
$rootScope.$on('$firebaseSimpleLogin:login', function (e, authUser) {
console.log("trigger userservice - $firebaseSimpleLogin:login");
var query = $firebase(ref.startAt(authUser.uid).endAt(authUser.uid));
query.$on('loaded', function () {
setCurrentUser(query.$getIndex()[0]);
});
});
$rootScope.$on('$firebaseSimpleLogin:logout', function() {
delete $rootScope.currentUser;
});
return User;
}]);
FIREBASE LOG—ON LOGIN (triggers):
p:0: Authenticating using credential: [object Object] firebase.js:34
p:0: {"r":4,"a":"auth","b":{"cred":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjEzOTYxODA0MzcsInYiOjAsImQiOnsicHJvdmlkZXIiOiJwYXNzd29yZCIsImlkIjoiMTQ1IiwidWlkIjoic2ltcGxlbG9naW46MTQ1IiwiZW1haWwiOiJjYXRlMzBAY2F0ZTMwLmNvbSIsIm1kNV9oYXNoIjoiM2QzOWIyZGU2Yjc1YjAyZWU0MWZlODkwMTQ2NzEwMTgiLCJzZXNzaW9uS2V5IjoiNjE0NTMzOWViZGU0YWQ0MGE4MjY5MzY4ODA2ZjIwNTcifSwiaWF0IjoxMzk2MDk0MDM2fQ.JAKEQH76MD9M4XsIDJDm0RLBuJ5rQzDgd4h1FiHQw78"}} firebase.js:34
p:0: from server: {"r":4,"b":{"s":"ok","d":{"auth":{"provider":"password","id":"145","uid":"simplelogin:145","email":"cate30@cate30.com","md5_hash":"3d39b2de6b75b02ee41fe89014671018","sessionKey":"6145339ebde4ad40a8269368806f2057"},"expires":1396180437}}} firebase.js:34
trigger userservice - $firebaseSimpleLogin:login userservice.js:49
0: event:/users:{"ep":"simplelogin:145","sp":"simplelogin:145"}:value firebase.js:34
0: event:/users:{"ep":"simplelogin:145","sp":"simplelogin:145"}:value firebase.js:34
triggered $firebaseSimpleLogin:login authcontroller.js:14
0: event:/users/145:default:value firebase.js:34
0: event:/users/145:default:value
FIREBASE LOG (ON REFRESH)—不触发
p:0: Browser went online. Reconnecting. firebase.js:34
p:0: Authenticating using credential: [object Object] firebase.js:34
p:0: Making a connection attempt firebase.js:34
c:0:0: Connection created firebase.js:34
c:0:0:0 Websocket connecting to wss://s-dal5-nss-18.firebaseio.com/.ws?v=5&ns=cc-1 firebase.js:34
c:0:0:0 Websocket connected. firebase.js:34
c:0:0: Realtime connection established. firebase.js:34
p:0: connection ready firebase.js:34
p:0: {"r":1,"a":"auth","b":{"cred":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjEzOTYxODA0MzcsInYiOjAsImQiOnsicHJvdmlkZXIiOiJwYXNzd29yZCIsImlkIjoiMTQ1IiwidWlkIjoic2ltcGxlbG9naW46MTQ1IiwiZW1haWwiOiJjYXRlMzBAY2F0ZTMwLmNvbSIsIm1kNV9oYXNoIjoiM2QzOWIyZGU2Yjc1YjAyZWU0MWZlODkwMTQ2NzEwMTgiLCJzZXNzaW9uS2V5IjoiNjE0NTMzOWViZGU0YWQ0MGE4MjY5MzY4ODA2ZjIwNTcifSwiaWF0IjoxMzk2MDk0MDM2fQ.JAKEQH76MD9M4XsIDJDm0RLBuJ5rQzDgd4h1FiHQw78"}} firebase.js:34
p:0: from server: {"r":1,"b":{"s":"ok","d":{"auth":{"provider":"password","id":"145","uid":"simplelogin:145","email":"cate30@cate30.com","md5_hash":"3d39b2de6b75b02ee41fe89014671018","sessionKey":"6145339ebde4ad40a8269368806f2057"},"expires":1396180437}}}
在学习thinkster教程第7部分时遇到了同样的问题。
注射User
固定。这在教程中就是下一个步骤(希望有人能够因为我的愚蠢而避免这个小障碍)。
但是一般来说,导致问题的原因是User
服务没有注入到页面上的任何控制器中,因此$rootScope.$on('$firebaseSimpleLogin:login' ...
侦听器没有被注册,因此没有触发