现在我有一个表,当前显示 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 中的用户和事件:
最佳答案
要获得您正在寻找的结果,请尝试使用 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/