Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat filter on boolean

I am trying to filter on a boolean value in an ng-repeat.

List of unregistered users:

    <h3>Unregistered Users</h3>     <div ng-repeat="user in users | filter:!user.registered">         <div class="row-fluid">         <div class="span2">           {{user.name}}         </div>       </div>     </div> 

List of registered users:

    <h3>Registered Users</h3>     <div ng-repeat="user in users | filter:user.registered">         <div class="row-fluid">         <div class="span2">           {{user.name}}         </div>       </div>     </div> 

Is there a good way to filter based on registered and !registered.

like image 249
lostintranslation Avatar asked Jul 15 '13 13:07

lostintranslation


People also ask

How do I filter in NG-repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.

How do I get an index in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

What does ng-repeat do?

Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

Where is the last element in NG-repeat?

You can use $last variable within ng-repeat directive. Take a look at doc. Where computeCssClass is function of controller which takes sole argument and returns 'last' or null .


2 Answers

filter by obj expression:

<h3>Unregistered Users</h3> <div ng-repeat="user in users | filter:{registered:false}">     <div class="row-fluid">     <div class="span2">       {{user.name}}     </div>   </div> </div> 

JSFiddle: http://jsfiddle.net/alfrescian/9ytDN/

like image 197
alfrescian Avatar answered Sep 21 '22 11:09

alfrescian


Create a method in the controller which returns true or false based on the logic you need and specify that function in the filter.

Something like this:

$scope.isRegistered = function(item) {   return item.registered; };  <h3>Unregistered Users</h3> <div ng-repeat="user in users | filter:!isRegistered ">     <div class="row-fluid">     <div class="span2">       {{user.name}}     </div>   </div> </div> 
like image 28
Giorgi Avatar answered Sep 21 '22 11:09

Giorgi