0

I have simple ng-repeat in my view that prints data from objects.

  <div
    ng-repeat="item in toDoItems |
               orderBy: 'createdAt'
               track by item.createdAt"
  >
    <b>Content:</b> {{item.content}}
  </div>

The object that I am printing looks like this:

     {
        "content" : "some content",
        "createdAt" : "1459401001460",
        "completed" : false
      },
      {
        "content" : "some content",
        "createdAt" : "1459401001325",
        "completed" : true
       }

I only want to print items that have value false in "completed" propery.

I have tried this but it would give me error: ng-repeat only objects with specific property value - custom filter?

1
  • just use ng-show :) below is my answer Commented Mar 31, 2016 at 5:20

4 Answers 4

1

you can use this format of filter.

var myapp = angular.module('app', []);
myapp.controller('Main', function ($scope) {
  $scope.data =[
       {
         "content" : "some content",
         "createdAt" : "1459401001460",
         "completed" : false
       },
      {
         "content" : "some content",
         "createdAt" : "1459401001325",
         "completed" : true
       },
       {
         "content" : "some content2",
         "createdAt" : "1459401001460",
         "completed" : false
       },
      {
         "content" : "some content",
         "createdAt" : "1459401001325",
         "completed" : true
       },
       {
         "content" : "some content3",
         "createdAt" : "1459401001460",
         "completed" : false
       },
      {
         "content" : "some content",
         "createdAt" : "1459401001325",
         "completed" : true
       }
 ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app = "app">
  <div ng-controller="Main">

<div >
    <table class="table table-bordered">
        <tr ng-repeat="d in data | filter: {completed: false }">
            <td>{{d.content}}</td>
        </tr>
    </table>
</div>
   </div>
</div>

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

Comments

1

Use this custom filter

app.filter('myFilter', function () {
    return function (items) {
        if (!items) {
            return true;
        }
        if (items.length == 0) {
            return items;
        }
        var newItems = [];
        angular.forEach(items, function(item, index){
           if(item.completed==true || item.completed=='true'){
               newItems.push(item)
           }
        })
        return newItems ;
    };
});

And your HTML is:

 <div ng-repeat="item in toDoItems | myFilter track by $index">
        <b>Content:</b> {{item.content}}
  </div>

2 Comments

May I ask you what is a good place to put custom filter files?
I worked lot on performance of the application. On Dev environment it doesn't matter where you place all JS files and how many files you create. I generally place all filters in separate files at module level. But on production environment try to minimize the HTTP requests. For that you have to minify JS code and place all your JS code into 2-3 files maximum.
0
<b ng-show='item.completed==false'>Content:</b> 
<span ng-show='item.completed==false'>{{item.content}}</span>

1 Comment

yes it can be, you have to create a custom filter for that and you are done
0

Use angular filter plugin for this, Change your code like below

Inject

<div
        ng-repeat="item in toDoItems | filterBy: ['completed']: true |
                   orderBy: 'createdAt'
                   track by item.createdAt"
      >
        <b>Content:</b> {{item.content}}
      </div>

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.