I have a problem on my filter when searching for a numerical value. I wish to have an exact search (using custum filter) on a "matricule" but it is a problem in my code and I do not know. this is my index :
<html>
<head>
<meta charset='utf-8'>
<script src="js/angular.js"></script>
<script src="js/app.js"></script>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<body ng-app="MyApp">
<div ng-controller="MyCtrl">
<h3>Filter by numeric value</h3>
<form class="form-inline">
<input ng-model="match.matricule" type="text" placeholder="Filter by matricule" autofocus>
</form>
<ul ng-repeat="friend in (result = (friends | exact: match ) ) ">
<li>{{friend.matricule}} - {{friend.name}} ({{friend.age}})</li>
</ul>
<p ng-show="result.length == 0"> Not Found </p>
{{result}}
</div>
</body>
</html>
this is my controller :
var app = angular.module("MyApp", []);
app.controller("MyCtrl", function($scope) {
$scope.friends = [
{ matricule :1 , name: "Peter", age: 20 , region : 'analamanga1'},
{ matricule :2 ,name: "Pablo", age: 55 , region : 'analamanga2'},
{ matricule :3 ,name: "Linda", age: 20 , region : 'analamanga3'},
{ matricule :4 ,name: "Marta", age: 37 , region : 'analamanga4'},
{ matricule :5 ,name: "Othello", age: 20 , region : 'analamanga5'},
{ matricule :11 ,name: "Markus", age: 32 , region : 'analamanga6'}
];
$scope.filterFunction = function(element) {
return element.name.match(/^Ma/) ? true : false;
};
})
app.filter('exact', function(){
return function(items, match){
var matching = [], matches, falsely = true;
// Return the items unchanged if all filtering attributes are falsy
angular.forEach(match, function(value, key){
falsely = falsely && !value;
});
if(falsely){
return items;
}
angular.forEach(items, function(item){
matches = true;
angular.forEach(match, function(value, key){ // e.g. 'all', 'title'
if(!!value){
matches = matches && (item[key] === value);
}
});
if(matches){
matching.push(item);
}
});
return matching;
}
});
sample : plnkr SAMPLE
THANKS