简体   繁体   English

如何在angular js中对对象数组应用过滤器?

[英]How to apply filter on array of Object in angular js?

I have an object of Student with names and other properties.我有一个带有名称和其他属性的 Student 对象。 I need to filter based on the name.我需要根据名称进行过滤。 Name will be passed as input text box.名称将作为输入文本框传递。 How to do it in angular.js如何在 angular.js 中做到这一点

$scope.Students = [ {Name:'David', id : '1010', Age: '20'} ,{Name:'John', id : '1020', Age: '21'} ,{Name:'Scarlett', id : '1020', Age: '21'} ]
const students = [ 
    {Name:'David', id : '1010', Age: '20'},
    {Name:'John', id : '1020', Age: '21'},
    {Name:'Scarlett', id : '1020', Age: '21'} 
]

const data = students.filter(function(item){
    return (item.Name === 'David');
});

console.log('data value: ', JSON.stringify(data));

You can filter using the $filter from angular js您可以使用 angular js 中的 $filter 进行过滤

var value = $scope.studentname;
var selected = $filter('filter')($scope.Students, {Name: value}, true),env;
$scope.$apply(function () {
    $scope.SelectedStudent = selected[0];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM