3

On client side I have $scope.loggedInUser, which refers to mongoose user schema. Another schema I'm using is a conversation schema. Every user can join conversation, in that case he will be added to conversation.participants array, which is defined like that:

var conversationsSchema = new Schema({
    participants: {type: Array, default: []}
});

I want to display only conversation with current user (i.e. loggedInUser) in participants array. I tried

ng-repeat="conversation in conversations" ng-if="conversation.participants.indexOf(logged_in_user) > -1"

but I dodn't see any. How can I check if element exists in array in ng-if (or generally in angular) correctly?

2
  • Do you have the same definition of conversation obj and logged_in_user obj? Commented Mar 22, 2015 at 18:15
  • @SatyamKoyani conversation.participants is an array of users objects. The point of filtering is actually to find if one of the users in this array is a currently logged in user. Commented Mar 22, 2015 at 21:22

1 Answer 1

2

You could use a filter like

ng-repeat="conversation in conversations | filter:logged_in_user"

I'm not sure if the view side implementation will dig into the nested collection, you might have to filter it in the controller

filteredConversations  = $filter(this.conversations,
{name:logged_in_user},doFiltering);

where do filtering is a method to do the actual work, something like:

function (actual, expected) {
return actual.participants.indexOf(expected) > -1;
}

be sure to inject $filter into your controller if you do it controller side.

Sign up to request clarification or add additional context in comments.

1 Comment

here is it possible only through viewside ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.