Firebase / Angular JS仅显示登录用户创建的firebase条目

时间:2016-07-21 23:48:13

标签: javascript angularjs firebase firebase-realtime-database

现在我有一个表,当前显示所有条目形成firebase中的“事件”节点。

但是,我只想显示登录用户创建的事件。现在他们正在显示所有用户创建的事件。

我猜我可以在标签中的ng-repeat之后使用ng-if指令,但是,我不知道该放入什么内容。

这是我的表:

<table class="table table-striped">
<thead>
  <tr>
    <th>Title</th><th>Location</th> <th>Actions</th>
  </tr>
</thead>
<tbody>
  <tr scope="row" ng-repeat="event in events | reverse" >
    <td>{{event.title}}</td>
    <td>{{event.location}}</td>
    <td><button class="btn btn-primary" ng-click="events.$remove(event)"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button></td>
  </tr> 

</tbody>

用户对象如下所示:

{
"provider": "password",
"uid": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
"token":
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6Im1pY2hhZWwubGFyaXZpZXJlMTk3M0BnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImlhdCI6MTQ2OTEyNTYyOSwidiI6MCwiZCI6eyJwcm92aWRlciI6InBhc3N3b3JkIiwidWlkIjoiNmY5ZmM0NTUtNTZmZS00MDBkLWI1MGItMWE2NzM2Zjg4NzRhIn19.yIzzV7Or7tUlXi-sSWeioNx6LLoQ0U9qnW1X06rpSmA",
"password": {
"email": "xxxxxx.xxxxxx1234@gmail.com",
"isTemporaryPassword": false,
"profileImageURL": "https://secure.gravatar.com/avatar/5f9effbf8cbea69792c595079cf25d38?d=retro"
},
"auth": {
"provider": "password",
"uid": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
"token": {
  "email_verified": false,
  "email": "xxxxxx.xxxxxx1234@gmail.com",
  "exp": 1469212029,
  "iat": 1469125629,
  "sub": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
  "auth_time": 1469125629,
  "firebase": {
    "identities": {
      "email": [
        "xxxxxx.xxxxxx1234@gmail.com"
      ]
    }
  }
}
},
"expires": 1469212029
}

我的控制器看起来像这样:

angular.module('myApp').controller('ChatCtrl', function($scope, user,
Ref, $firebaseArray, $timeout) {

console.dir('user: ' + JSON.stringify(user));

// synchronize a read-only, synchronized array of events, limit to most recent 10
$scope.events = $firebaseArray(Ref.child('events').limitToLast(10));
// display any errors
$scope.events.$loaded().catch(alert);

// provide a method for adding a event
$scope.addEvent = function(newEvent) {


    if (newEvent) {
        // push a event to the end of the array
      $scope.events.$add({
        title:     newEvent.title,
        location:  newEvent.location,
        createdBy: user.uid,
        createdAt: Firebase.ServerValue.TIMESTAMP
      })
      // display any errors
            .catch(alert);
    }

  };

function alert(msg) {
    $scope.err = msg;
    $timeout(function() {
        $scope.err = null;
      }, 5000);
  }
});

这就是firebase中用户和事件的样子:

enter image description here

1 个答案:

答案 0 :(得分:1)

要获得您正在寻找的结果,请尝试使用angularjs过滤器。

在你的控制器中添加一个名为

的函数
<tr scope="row" ng-repeat="event in events | reverse" >

此功能将作为一个过滤器,通过将事件的创建对象与用户的uid进行比较,只允许通过当前用户创建的事件。

然后在你的html中更改这一行

<tr scope="row" ng-repeat="event in events | reverse | filter:filterByUID" >

merge

这告诉angularjs您希望使用我们在控制器中定义的过滤器过滤您的项目。

修改:以下是使用自定义过滤器的参考:AngularJS : Custom filters and ng-repeat