Filter by name after applying a filter that changes characters

I came across a situation where I need to filter elements created with the 'ng-repeat' directive, but I applied a custom filter that changes one character to another and vice versa for each element created.

Then, if I look for a new character that has been replaced, the filter will not find it - unless I look for the old one.

How to apply this input filter after using my custom filter that changes characters?

In my custom brNumber filter, the points are swapped with commas and vice versa, so if I search for a point, the filter will only find those that have a comma.

Fiddle

<HTML>

<div ng-app="myApp" ng-init=" person= [ {firstName:'Johnny',lastName:'Dowy'}, {firstName:'Homem,25',lastName:'Cueca,Suja'}, {firstName:'Alleria.Donna',lastName:'Windrunner'} ];" > First Name: <input type="text" ng-model="firstName"> <br /> The persons objects have: | <span ng-repeat="i in person | orderBy: 'firstName' | filter:firstName">{{ ( i.firstName + ' ' + i.lastName ) | brNumber }} | </span> 

{Javascript.js}

 app.filter( 'brNumber', function() { return function( text ) { string = text.toString(); returnString = ''; for ( i = 0; i < string.length; i++ ) { returnString += string[i] === ',' ? '.' : ( string[i] === '.' ? ',' : string[i] ); } return returnString; } }); 
+7
javascript html angularjs
source share
1 answer

You can wrap the filtered value in the same function as the filter. Check out https://jsfiddle.net/5Lcafzuc/3/

 /// wrap filter argument in function ng-repeat="i in person | orderBy: 'firstName' | filter: replace(firstName)" /// add function in scope and use it in display filter var replace = function(text) { if(!text) { return false; } if(text.indexOf(".") >= 0) { text = text.replace(".", ","); } else if(text.indexOf(",") >=0) { text = text.replace(",", "."); } return text; } app.controller('myCtrl', function($scope) { $scope.replace = replace; }); app.filter( 'brNumber', function() { return function(text) { return replace(text); } }); 
+4
source share

All Articles