【问题标题】:AngularFire - Removing an object from an array, with a twistAngularFire - 从数组中删除一个对象,有一个扭曲
【发布时间】:2013-10-02 18:23:52
【问题描述】:

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

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() {}; 我遇到的问题是将登录的特定用户传递给此函数,以便我可以拼接该索引。

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

【问题讨论】:

    标签: angularjs firebase angularfire


    【解决方案1】:

    使用对象而不是数组可能会更好,而 angularFire 隐式服务可以做到这一点。

    您的用户名是唯一的吗?如果是这样,您可以执行以下操作:

    $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 绑定对象):

    $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,我使用的是 angularFireAuth 和 angularFireCollection。我正在使用 angularFireAuth.initialize(url, {scope: $scope, name: "user"});不知道这一切是如何运作的。
    • 所以,我将 angularFire 加入了组合中。并且使用此代码,只要有人登录 {{loggedIn}} 是一个空数组,而 {{currentUser}} 显示当前登录的用户,它就会出现。
    • {{loggedIn}} 是一个对象而不是数组。尝试在 $timeout 块中包围 '$scope.loggedIn[$scope.currentUser] = true',这可能是 Angular 没有在 auth 回调中获取对 $scope 的更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2012-07-24
    • 2016-11-08
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多