0

Right now I have a table that is currently showing all the entries form an "events" node in firebase.

However, I only want to show the events created by the logged in user. Right now they are showing events created by all users.

I'm guessing I might be able to use an ng-if directive after the ng-repeat in the tag, however, I am not sure what to put into it.

This is my 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>

The user object looks like so:

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

My controller looks like this:

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

This is what the users and events look like in firebase:

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Livi17
  • 1,620
  • 3
  • 25
  • 43

1 Answers1

1

To get the results that you're looking for, try using an angularjs filter.

In you controller add a function called

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

This function will act as a filter that only let's through events that were created the current user by comparing the event's createdBy to the user's uid.

Then change this line in your html

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

To

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

This tells angularjs that you want to have your items filtered with the filter we defined in the controller.

Edit: Here's a reference on using custom filters: AngularJS : Custom filters and ng-repeat

Community
  • 1
  • 1
Davis
  • 1,161
  • 7
  • 8
  • I tried putting that function in my controller, but the function doesn't seem to be getting called. I added a console.log(); within the if statement, and outside of the it statement, within the function, and nothing appears in the console. `var filterByUID = function(event) { if (event.createdBy === user.uid) { console.log("if condition met"); return true; } console.log("if condition NOT met"); return false; };` All of the events still show up regardless of which user created them. – Livi17 Jul 22 '16 at 00:21
  • I got it to work using: `$scope.filterByUID = function(event) {...` – Livi17 Jul 22 '16 at 00:26
  • 1
    Sorry. I totally forgot about setting the function as a property of the $scope. I edited the answer and I'm glad that worked out. – Davis Jul 22 '16 at 00:30