用火力地堡引用的变量不工作从Array AngularFire $删除项目 [英] AngularFire $remove item from Array using a variable in Firebase reference does not work

查看:182
本文介绍了用火力地堡引用的变量不工作从Array AngularFire $删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在挣扎以下问题:

I've been struggling with the following problem:

我想从与$一个火力地堡阵列删除后项目中删除这是我在一个角服务(厂)已实施AngularFire方法。这篇文章是事件的孩子,所以为了将其删除我要通过这个服务相关的事件,其中我要删除的帖子一个说法。

I'm trying to delete a 'Post' item from a Firebase Array with the $remove AngularFire method which I have implemented in a Angular Service (Factory). This Post is a child of 'Event', so in order to delete it I have to pass this Service a argument with the relevant Event of which I want to delete the post.

这是我的控制器:

app.controller('EventSignupController', function ($scope, $routeParams, EventService, AuthService) {
  // Load the selected event with firebase through the eventservice
  $scope.selectedEvent = EventService.events.get($routeParams.eventId);

  // get user settings
  $scope.user =  AuthService.user;
  $scope.signedIn = AuthService.signedIn;

  // Message functionality
  $scope.posts = EventService.posts.all($scope.selectedEvent.$id);

  $scope.post = {
    message: ''
  };
  $scope.addPost = function (){
    $scope.post.creator = $scope.user.profile.username;
    $scope.post.creatorUID = $scope.user.uid;
    EventService.posts.createPost($scope.selectedEvent.$id, $scope.post);
  };

  $scope.deletePost = function(post){
    EventService.posts.deletePost($scope.selectedEvent.$id, post);
    // workaround for eventService bug:
    // $scope.posts.$remove(post);
  };
});

这是我的服务(厂):

And this is my Service (Factory):

app.factory('EventService', function ($firebase, FIREBASE_URL) {
  var ref = new Firebase(FIREBASE_URL);
  var events = $firebase(ref.child('events')).$asArray();

  var EventService = {
    events: {
      all: events,
      create: function (event) {
        return events.$add(event);
      },
      get: function (eventId) {
        return $firebase(ref.child('events').child(eventId)).$asObject();
      },
      delete: function (event) {
        return events.$remove(event);
      }
    },
    posts: {
      all: function(eventId){
        var posts = $firebase(ref.child('events').child(eventId).child('posts')).$asArray();
        return posts;
      },
      createPost: function (eventId, post) {
        // this does work
        var posts = $firebase(ref.child('events').child(eventId).child('posts')).$asArray();
        return posts.$add(post);
      },
      deletePost: function (eventId, post) {
        // this does not work
        var posts = $firebase(ref.child('events').child(eventId).child('posts')).$asArray();
        return posts.$remove(post);
      }
    }
  };

  return EventService;
});

当我尝试删除该链接标记只是冻结,并没有错误记录出现在控制台。而如果我叫$我$ scope.posts直接在我的控制器它可以神奇地运行。此外我的文章不是从我的火力地堡数据库中删除删除。

When I try to delete the link tag just freezes and no error logging appears in the console. While if I call $remove on my $scope.posts directly in my controller it magically works.. Furthermore my Post is not removed from my Firebase DB.

另一个奇怪的是,CreatePost使用相同的建设工作完全正常。

Another weird thing is that 'CreatePost' works perfectly fine using the same construction.

我的观点:

 <div class="col-xs-8 col-xs-offset-2 well">
        <form ng-submit="addPost()" ng-show="signedIn()">
          <input type="text" ng-model="post.message" />
          <button type="submit" class="btn btn-primary btn-sm">Add Post</button>
        </form>
        <br>
        <div class="post row" ng-repeat="post in posts">
          <div>
            <div class="info">
                {{ post.message }}
            </div>
            <div>
              <span>submitted by {{ post.creator }}</span>
              <a href="" ng-click="deletePost(post)" ng-show="user.uid === post.creatorUID">delete</a>
            </div>
            <br>
          </div>
        </div>
      </div>

P.S。我不太确定我的'服务'在尽可能最好的方式实现的。我找不到做多火力点呼叫另一种解决方案

P.s. I'm not too sure that my 'Service' is implemented in the best possible way.. I couldn't find another solution for doing multiple firebase calls

var posts = $firebase(ref.child('events').child(eventId).child('posts')).$asArray();

我EventService的邮政部件内,因为这取决于每个CRUD操作EVENTID。任何想法将是非常值得欢迎的:)

within the Post part of my EventService, because it depends on eventId in each CRUD operation. Any ideas would be very welcome :)

推荐答案

对我来说,最简单的方法是使用这样的:

The easiest way for me was to use this:

var ref= new Firebase('https://Yourapp.firebaseio.com/YourObjectName');
ref.child(postId).remove(function(error){
    if (error) {
    console.log("Error:", error);
  } else {
    console.log("Removed successfully!");
  }
});

这篇关于用火力地堡引用的变量不工作从Array AngularFire $删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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