1

I would like to filter the msgs data by not displaying some message of corresponding userid. In the example below it only shows the message from Paul (userid: 11) & Kate (userid:12).

What I would like to do is to filter more than just one userid. Something like :

{userid:["!10", "!11"]};

which should display only messages from userid 12 (In this case).

Plunker : http://plnkr.co/edit/464FVab41YoV2BGFkgWt?p=preview

$scope.msgs = [{name:"John", userid:10, text:"Hello"}, {name:"Paul", userid:11, text:"hi"}, {name:"Kate", userid:12, text:"Hey"}];
$scope.filter = {userid:"!10"};

<div ng-repeat="msg in msgs | filter:filter">
   {{msg.name}} ({{msg.userid}}) : {{msg.text}}
</div>

1 Answer 1

3

You are half way there. You just need to add a filter that takes out the undesired user ids.

angular.module('app', [])
    .controller('mainCtrl', function($scope) {
        
        $scope.msgs = [
            {"name":"John", "userid":"10", "text":"Hello"},
            {"name":"Paul", "userid":"11", "text":"Hi"},
            {"name":"Pete", "userid":"12", "text":"'Allo"},
        ];
                  
        $scope.idsToTakeout = ['11', '12'];
        $scope.filterOutUserIds = function(i) {
            return ($scope.idsToTakeout.indexOf(i.userid) === -1);
        };
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">
<div ng-controller="mainCtrl">
<div ng-repeat="msg in msgs | filter:filterOutUserIds">
    id: {{ msg.userid }}, message: {{msg.text}}
</div>

</div>
</div>

I also created a plunker.

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

Comments

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.