问题
I have modified the AngularFire chat demo quite a bit. I am stuck at this point. I have created an array on Firebase of users when they log in, like so...
var url2 = 'https://<yoursite>.firebaseio.com/users';
$scope.$on("angularFireAuth:login", function(){
$scope.loggedIn.add({newUser: $scope.user.username});
});
$scope.loggedIn = angularFireCollection(new Firebase(url2).limit(50));
and for the html...
<ul>
<li ng-repeat="x in loggedIn | unique:'newUser' ">{{x.newUser }}</li>
</ul>
Since each item that is repeated is not a link, and there is no button for each row of the li, (which would not be good anyway) it seems the only thing that would work is hooking into the $scope.$on("angularFireAuth:logout", function() {}; The problem I am having though is passing the specific user that is logged in through to this function so I can splice that index.
Also, since the list is being populated from Firebase it seems that splice would have to go all the way up to Firebase and remove that index. Any ideas?
回答1:
It might be better to use an object instead of an array, and the angularFire
implicit service to do this.
Are your usernames unique? If so, you can do something like:
$scope.loggedIn = {};
angularFire(new Firebase(url), $scope, 'loggedIn');
$scope.$on('angularFireAuth:login', function() {
$scope.currentUser = $scope.user.username;
$scope.loggedIn[$scope.currentUser] = true;
});
$scope.$on('angularFireAuth:logout', function() {
delete $scope.loggedIn[$scope.currentUser];
});
Otherwise, you can use ref.push().name() to generate a unique ID for every user (and still use angularFire to bind an object):
$scope.loggedIn = {};
angularFire(new Firebase(url), $scope, 'loggedIn');
$scope.$on('angularFireAuth:login', function() {
$scope.currentUser = new Firebase(url).push().name();
$scope.loggedIn[$scope.currentUser] = true;
});
$scope.$on('angularFireAuth:logout', function() {
delete $scope.loggedIn[$scope.currentUser];
});
来源:https://stackoverflow.com/questions/19144032/angularfire-removing-an-object-from-an-array-with-a-twist