Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs: $filter is undefined when trying to get filtered data

Tags:

angularjs

Since days im trying to get this running: With the following snippet i want to filter some persons and after onchange was fired receive the objects which have been filtered. see this Code live here: http://jsbin.com/isojof/1/

Any idea?

There is noch $filter Object yet...but how to create one? $filter('filter') is obviously not working!

<html ng-app>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>

</head>


<body ng-controller="List">

    Search: <input ng-change="getData(names, query)" ng-model="query">
    Search: <select ng-change="getData(names, query2)" ng-model="query2">
    <option></option>
    <option>Berlin</option>
    <option>Hamburg</option>
</select>
 <div>
    <ul class="names" >
        <li ng-model="item" " ng-repeat="name in names | filter:query | filter:query2">
            {{name.firstname}}, {{name.lastname}}, {{name.age}}, {{name.location}}
        </li>
    </ul>
</div>
    <script type="text/javascript">
    function List($scope) {
        $scope.names = [
        {"firstname": "Carl",
        "lastname": "Luyk",
        "age": 20,
        "location":"Berlin"},
        {"firstname": "Carl",
        "lastname": "Moreen",
        "age": 20,
        "location":"Hamburg"},
        {"firstname": "Tom",
        "lastname": "Luyk",
        "age": 25,
        "location":"New York"},
        {"firstname": "Caren",
        "lastname": "Tilt",
        "age": 20,
    "location":"Paris"},
        {"firstname": "Caren",
        "lastname": "Orail",
        "age": 30,
        "location":"Hamburg"},
        ];
$scope.getData = function (names, query) {
  $scope.queryData = $filter('filter')(names, query);
  console.log($scope.queryData);
};



    }
    </script>

</body>
</html>  
like image 407
Jurudocs Avatar asked May 23 '13 06:05

Jurudocs


1 Answers

You just need to inject $filter into your controller:

Change

function List($scope) { 

to

function List($scope, $filter) { 

http://jsbin.com/isojof/2/

like image 180
Dogbert Avatar answered Oct 03 '22 20:10

Dogbert