8

I have an ng-repeat using a filter like this:

#1
<div class="contestReports" ng-repeat="contest in contests | filter:{votingOver:true}">
    <contestreport></contestreport>
</div>

I want to allow the customer to be able to filter it so I have assigned the filter to a variable like this:

#2
<div ng-init="reportFilter = {votingOver:true}"></div>
<div class="contestReports" ng-repeat="contest in contests | filter:reportFilter">
    <contestreport></contestreport>
</div>

Code #1 is working but Code #2 is not and I'm not sure why.

5
  • Please have a look at this plnkr.co/edit/nFFHmUA7RdxMIPV9VUDC?p=preview Commented Feb 18, 2016 at 7:35
  • That is searching by plaintext and not an object filter Commented Feb 19, 2016 at 2:57
  • in general ngInit is not a good practice to use anyway - docs.angularjs.org/api/ng/directive/ngInit, or it's more of a theoretical question for you? Commented Feb 20, 2016 at 19:56
  • Works in this DEMO on JSFiddle. Commented Feb 21, 2016 at 18:21
  • @Jordash don't you interested in give a bounty? Have you checked answers? Commented Feb 28, 2016 at 16:31

4 Answers 4

3
+25

Did you also try to wrap ng-init

<div ng-init="(reportFilter = '{votingOver:true}')"></div>

However, as I've stated in my comment earlier - angularjs documentation states that using ngInit is a bad practice in mostly all cases. So that should not be a solution to your problem if possible.

And your #2 code actually works, please check this plunk - http://plnkr.co/edit/dBDyYPd3ZoUVdXngu52t?p=preview

//html
  <div ng-init="reportFilter = {votingOver:false}"></div>
<div class="contestReports" ng-repeat="contest in contests | filter:reportFilter">
  {{contest | json}}
</div>

  </div>

//js in controller
  $scope.contests = [
    {id:1, title:'1', votingOver:false},
    {id:2, title:'2', votingOver:true},
    {id:3, title:'3', votingOver:true}
    ];
Sign up to request clarification or add additional context in comments.

Comments

2

Using ng-init is not a good way to go with(as you can see angular docs has added warning text in red). As you know it is going to evaluate on initial rendering only(as ng-init evaluates its expression in preLink phase).

By looking at your code, it seems like reportFilter variable is going to be an common object for all contests. Correct me if I'm wrong?

I'd add this value inside a controller only once like below.

//place this inside your controller
$scope.reportFilter = { votingOver : true };

And use directly on view itself.

<div class="contestReports" ng-repeat="contest in contests | filter:reportFilter">
    <contestreport></contestreport>
</div>

Comments

1

Better way is to save the result of applying filter to the json data in a variable before ng-repeat reads items in the jsonArray like below.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <div ng-init="contests=
        [{name:'bad apple', votingOver:false},
         {name:'pleasurebuilder', votingOver:true},
        {votingOver:true}]">
  </div>
  <div ng-init="reportFilter = {votingOver:true}">
  </div>
  <div ng-repeat="contest in revoluza = (contests | filter: reportFilter)">
    {{contest}}
  </div>
  <hr>
</div>

Comments

1

Code 2 is not working because you have to create custom filter while you are only intializing div not creating filter

Code 1 is working because you are using proper syntax for default filter that can be compared with any object just like you did

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.