salamanka44 salamanka44 - 1 year ago 94
AngularJS Question

How to filter elements in ng repeat in Angular

I have an array of elements with names and values that I want to filter in a ng-repeat tag. My array has elements like that :

$scope.extensions = [{
name: "extension1.00",
value: 1234587
}, {
name: "extension1.02",
value: 665873698742136544
}, {
name: "extension2.00",
value: 11
}........ //other elements

And my ng-repeat tag is the following :

<div ng-repeat = "extension in ( extensions | filter : myFilter( | orderBy: 'name' ) ">

And what I want to do is to write a custom filter to display ONLY elements whose name doesn't end with ".00". So, in the example above, I should only see "extension1.02" and not the two others.

So, I tried to write the simple filter just to not display the first element ("extension1.00") :

$scope.myFilter = function(name) {
return function(item) {
var result = true;
if (item === "extension1.00") {
return false;
} else {

return result;

But it didn't work at all...
Finally, Here is my code :

I tried to write my own filter but I have no idea how can I achieve this.
Can you help me please to write that filter? Thank you!

Answer Source

I have made a working example here --> click here

The biggest mistake you did, was to hold the filter function inside the controller.

My filter function:

app.filter('myFilter', function(){
 return function(items, str){
  var filtered = [];
  angular.forEach(items, function(item){
    if ('.')+1) === str){
return filtered;}}); 

Check out the link to the codepen.. it shows a bit more custom filter that you can work with :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download