AngularFire - 从阵列中删除的对象,与一捻 [英] AngularFire - Removing an object from an array, with a twist
问题描述
我修改了AngularFire聊天演示了不少。我停留在这一点上。我建立了用户的火力地堡数组登录时,像这样......
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));
和为HTML ...
and for the html...
<ul>
<li ng-repeat="x in loggedIn | unique:'newUser' ">{{x.newUser }}</li>
</ul>
由于每个重复项目是不是一个链接,并没有为李,(这不会是反正好)的每一行没有按钮,它似乎只是将工作被钩住$范围内的事情。$上(angularFireAuth:注销功能(){};我有但问题是通过被记录在通过这个功能特定的用户,所以我可以拼接该指数
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?
推荐答案
这可能是更好的使用对象,而不是一个数组,而 angularFire
隐含的服务做这一点。
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];
});
另外,你可以使用ref.push()名称()来生成每个用户一个唯一的ID(并仍然使用angularFire绑定的对象):
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];
});
这篇关于AngularFire - 从阵列中删除的对象,与一捻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!