如何从angularjs中的文本框中过滤数据

时间:2015-11-18 09:12:23

标签: javascript angularjs

**嗨,我正在从文本框中过滤数组数据,但是代码无法正常工作,任何人都可以帮助我。来自后端的数据

 self.AdminLineDetails = function(data) {
   $scope.details = [];
   $scope.details = data.GoalData;
   console.log(data);
 }
<div class="row">
  <div class="col-md-12">
    <input ng-model="query" type="text" class="form-control" placeholder="Filter by name or number">
  </div>
  <div>
    <tbody>
      <tr ng-repeat="detail in details|filter:query">
        <td><a href="#">{{detail.firstName}}</a>
        </td>
        <td><a href="#">{{detail.lastName}}</a>
        </td>
        <td><a href="#">{{detail.mdn}}</a>
        </td>
      </tr>

    </tbody>
  </div>

**

2 个答案:

答案 0 :(得分:1)

您可以指定要尝试过滤的属性,执行类似

的操作
 <tr ng-repeat="detail in details|filter: {firstName: query}">

答案 1 :(得分:0)

<input type="text" ng-model="search">
<ul ng-repeat="oneauth in authorisations[0]">
    <li ng-repeat="entry in oneauth | nameFilter:search">{{entry.auth.name}}</li>
</ul>

JS

var app = angular.module('myapp', [], function () {});

app.controller('AppController', function ($scope) {    
    $scope.authorisations = [{
        "authorisations":[
        {
            "auth":{
                "number":"453",
                "name":"Apple Inc."
            }
        },
        {
            "auth":{
                "number":"123",
                "name":"Microsoft Inc."
             }
        }]
    }];
});

app.filter('nameFilter', function(){
    return function(objects, criteria){
        var filterResult = new Array();
        if(!criteria)
            return objects;

        for(index in objects) {
            if(objects[index].auth.name.indexOf(criteria) != -1) // filter by name only
                filterResult.push(objects[index]);
        }
        console.log(filterResult);
        return filterResult;  
    }    
});

检查此样本

http://jsfiddle.net/yctchgnk/