Reputation: 9165
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>
Upvotes: 0
Views: 8897
Reputation: 39
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!
Upvotes: 3
Reputation: 680
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.
Upvotes: 7