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

标签 javascript angularjs firebase firebase-realtime-database

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

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

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

这是我的 table :

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

我的 Controller 如下所示:

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

最佳答案

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

在你的 Controller 中添加一个名为的函数

$scope.filterByUID = function(event) {
    if (event.createdBy === user.uid) {
        return true;
    }
    return false;
}

此函数将充当过滤器,通过将事件的 createBy 与用户的 uid 进行比较,仅允许我们浏览当前用户创建的事件。

然后更改 html 中的这一行

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

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

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

编辑:这是有关使用自定义过滤器的引用:AngularJS : Custom filters and ng-repeat

关于javascript - Firebase/Angular JS 仅显示由登录用户创建的 Firebase 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38516087/

相关文章:

javascript - 使用jquery调整div左上角的大小

c# - 将 C# 类渲染为 javascript

javascript - 默认函数参数没有按我的预期工作

带有 Gradle 插件版本 3.6.1 和 Gradle 版本 5.6.4 的 Android Studio 3.6.1 无法正常工作

ios - 使用适用于 iOS 的 FirebaseUI 访问数据库

javascript - 当用户单击 slick-header-menuitem 下拉列表时,如何将所有数据放入 slickgrid。?

java - 将 $routeParams 从 angularjs 传递到 spring-MVC Controller

javascript - 如何通过添加/删除 Angular 输入来管理焦点/光标位置?

javascript - AngularJS 指令 'drawing' 需要错误 : Controller 'drawing' ,,无法找到

swift - Firebase 删除值无法正常工作