AngularJS - $filter inside controller

by santhosh lanka

HTML

<div ng-app>
    <div ng-controller="MyCtrl">
        <input type="text" ng-model="search">
        <table>
            <tbody>
                <tr ng-repeat="item in items">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

JavaScript

function MyCtrl($scope, $filter)
{
    $scope.items = [
        {id:1, name:'John'},
        {id:2, name:'Steve'},
        {id:3, name:'Joey'},
        {id:4, name:'Mary'},
        {id:5, name:'Marylin'}];
    
    $scope.items2 = $scope.items;
    $scope.search1 = [1];
    $scope.$watch('search', function(val)
    { 
        $scope.items = $filter('filter')($scope.items2, val);
    });
};