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

how to filter the data from text box in angularjs

本文关键字:数据 文本 过滤 angularjs      更新时间:2023-09-26

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

 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>

**

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

 <tr ng-repeat="detail in details|filter: {firstName: query}">
<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/