Alex Man
Alex Man

Reputation: 4886

case sensitive filter in angular js without using user defined filter

can anyone please tell me how to do a case sensitive filter without using user defined filter in angularjs, see here i want to print all the names except john, but when i put filter:{item: '!john'} it removes john, johnmathew as well as johnny, but i needs only john to be removed.

html

<div ng-app='myApp' ng-controller="Controller">
    <div ng-repeat="val in items | filter:{item: '!john'}">{{val.item}}</div>
</div>

script

var app = angular.module('myApp', []);
app.controller('Controller', function ($scope) {

    $scope.items = [{
        item: 'john'
    }, {
        item: 'sunny'
    }, {
        item: 'johnmathew'
    }, {
        item: 'lenu'
    }, {
        item: 'binu'
    }, {
        item: 'johnny'
    }];
});

JSFiddle

Upvotes: 3

Views: 2673

Answers (1)

Dieterg
Dieterg

Reputation: 16368

If you're using a newer version of AngularJS than in your fiddle you could just add :true for an exact match.

<div ng-repeat="val in items | filter:{item: '!john'}:true">{{val.item}}</div>

Fiddle

If you're not using a newer version you'll have to create your own filter that does the check.

HTML

<div ng-repeat="val in items | objFilter:'item':'john'">{{val.item}}</div>

JS

app.filter('objFilter', function() {
    return function(input, prop, value) {
        var retValue = [];
        input.forEach(function(item) {
            if(item[prop] && item[prop] !== value) {
                 retValue.push(item);   
            }
        });
        return retValue;
    };
});

Custom filter fiddle

Upvotes: 5

Related Questions