Angula Multi Keyword Filter - Andrew Pougher

Angula Multi Keyword Filter - Andrew Pougher

by Andrew Pougher

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js"></script>
<div ng-app ng-controller="MyCtrl" class="container">
     <h1 class="lead">Filtering Multi Key Words</h1>

    <p>
        <label>filter</label>
        <input data-ng-model="search.$" type="text" class="form-control search">
    </p>
    <ul>
        <li data-ng-repeat="human in peeps | filter:search:PougherMultiFltr">{{human.email}} {{human.name}}</li>
    </ul>
</div>

CSS

.search {
    font-size:22px;
    color:#333333
}

JavaScript

function MyCtrl($scope, $timeout) {
    $scope.peeps = [{
        email: '[email protected]',
        name: 'Mr X'
    }, {
        email: '[email protected]',
        name: 'Mr M'
    }, {
        email: '[email protected]',
        name: 'Ms White'
    }];

    $scope.PougherMultiFltr = function (vl, fm) {
        // Get a list of predicates
        var words = fm.split(' ');
        var t = false;
        angular.forEach(words, function (actual) {
            //case-insensitive
            if (angular.lowercase(vl).indexOf(angular.lowercase(actual)) !== -1) {

                t = true;
            }
        });
        $timeout(function () {}, 350);
        return t

    };
}