0

i have trouble to figure out how i can receive the filtered data after the change event took place. My code structure looks like the following. THe alert is fired but how to move on?

<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()" ng-model="query">
    Search: <select ng-change="getData()" 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(){
    //here I would like to get the data structured like $scope.names..is that possible?
    alert('changed');

    }

    }
    </script>

</body>
</html> 
3

2 Answers 2

7

Try this:

Search: <input ng-change="getData(names, query)" ng-model="query">

And inside your Controller:

$scope.getData = function (names, query) {
  $scope.queryData = $filter('filter')(names, query));
};

So $scope.queryData is now your results collection.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot...but it raises the error "$filter is undefined" any idea?
@Jurudocs not sure if you ever figured it out, but that is because you need to inject $filter into your controller.
This will throw a syntax error due to an unclosed parenthesis on line 2.
3

The reason you're getting "$filter is underfined" is because you haven't injected it into your controller. When you define your controller, do:

app.controller('list', function($scope, $filter) {
  $scope.getData = function (names, query) {
    $scope.queryData = $filter('filter')(names, query));
  };
}

You can also define your search in your controller.

$scope.queryData = $filter('filter')(array, search-expression));

Hope that helps!

3 Comments

So will search-expression be equal to query?
$filter('filter')(array, search-expression)) The extra closing bracket, is it a typo??
@UdaySawant Yeah, the fact it's in two answers is pretty sad. Downvoting until fixed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.