angular code js filter does not work for me

0

function MyCtrl($scope, $filter)
{
    $scope.items = [
        {id:71610067, name:'John'},
        {id:2, name:'Steve'},
        {id:3, name:'Joey'},
        {id:4, name:'Mary'},
        {id:5, name:'Marylin'}];
    
    $scope.items2 = $scope.items;
    
    $scope.$watch('search', function(val)
    { 
        $scope.items = $filter('filter')($scope.items2, val);
    });
};
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="data.js" ></script>
<body>

<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>



<p>The list will only consists of names matching the filter.</p>


</body>
</html>

Does someone help me know why my code does not work?

    
asked by Josue Gonzalez 02.06.2017 в 19:42
source

1 answer

3

I would do it easier without doing the filter in the controller

    <input type="text" ng-model="query">
    <table>
        <tbody>
            <tr ng-repeat="item in items | filter:{item.name:query}">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
            </tr>
        </tbody>
    </table>
    
answered by 02.06.2017 в 20:25