angularjs:尝试获取过滤数据时,$ filter未定义

时间:2013-05-23 06:00:34

标签: angularjs

由于几天我试图让这个运行:使用以下代码片段我想过滤一些人,并在onchange被触发后接收已经过滤的对象。请在此处查看此代码:http://jsbin.com/isojof/1/

有什么想法吗?

还有noch $ filter对象......但是如何创建一个呢? $ filter('filter')显然无效!

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

1 个答案:

答案 0 :(得分:83)

您只需要将$filter注入您的控制器:

更改

function List($scope) {

function List($scope, $filter) {

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