AngularFire - 从数组中移除一个对象,稍有不同 [英] AngularFire - Removing an object from an array, with a twist

查看:21
本文介绍了AngularFire - 从数组中移除一个对象,稍有不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 AngularFire 聊天演示做了很多修改.我被困在这一点上.我在用户登录时在 Firebase 上创建了一个数组,就像这样......

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...

<ul>
   <li ng-repeat="x in loggedIn | unique:'newUser' ">{{x.newUser }}</li>
</ul>

由于每个重复的项目都不是链接,而且 li 的每一行都没有按钮(无论如何都不好),似乎唯一可行的方法就是挂钩 $scope.$on("angularFireAuth:logout", function() {}; 我遇到的问题是将登录的特定用户传递给此函数,以便我可以拼接该索引.

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.

此外,由于列表是从 Firebase 填充的,似乎 splice 必须一直到 Firebase 并删除该索引.有什么想法吗?

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().name() 为每个用户生成唯一 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆