0

Please see the output and give the reason why is it working like this?? I am trying to use the filter for nested values and filter is giving random output.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<p>Type a letter in the input field:</p>

<ul>
  <li ng-repeat="x in names | filter:x.na.foo=0">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
});
</script>

<p>The list will only consists of names matching the filter.</p>


</body>
</html>

and the output is :

Type a letter in the input field:

Jani 0
Margareth 1
Hege 0
Joe 1
Gustav 0
Mary 0
Kai 1
The list will only consists of names matching the filter.

3 Answers 3

2

use the filter like this when you are filtering nested objects

<li ng-repeat="x in names | filter: {na: {foo: 0}}">

Demo

angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<p>Type a letter in the input field:</p>

<ul>
  <li ng-repeat="x in names | filter: {na: {foo: 0}}">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

</div>

 

<p>The list will only consists of names matching the filter.</p>


</body>
</html>

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

Comments

1

Through Controller:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  
  $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
    
     $scope.foofilter = function(data) { 
        return data.na.foo === 0 ;
   };
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
    
    <p>Type a letter in the input field:</p>

<ul>
  <li ng-repeat="x in names | filter: foofilter">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

  </body>

</html>

Comments

1

I have got you two code one to filter and one to order by,

OrderBy

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<input type="text" ng-model="search"/>

<ul>
  <li ng-repeat="x in names | orderBy:'-na.foo'" ">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
});
</script>

<p>The list will only consists of names matching the filter.</p>

Filter

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<input type="text" ng-model="search"/>

<ul>
  <li ng-repeat="x in names | filter:search">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
});
</script>

<p>The list will only consists of names matching the filter.</p>

Please use the one which would help you

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.