Angularjs Angular JS-如何过滤数组中的多个元素?

Angularjs Angular JS-如何过滤数组中的多个元素?,angularjs,angularjs-ng-repeat,Angularjs,Angularjs Ng Repeat,我一直在搞Angular.js,但我似乎无法解决这个问题 拿起下面的笔,尝试搜索全名Zoey White-过滤器工作正常,直到您开始键入“White”。我假设代码中的某些内容没有选择一种允许您一次过滤多个数组的“AND”函数 有人对解决这个问题有什么建议吗 选项1: 向用户添加fullName $scope.users = [ { firstName: "Camila", lastName: "Gilson", fullName: "Camila Gilson" }, { fir

我一直在搞Angular.js,但我似乎无法解决这个问题

拿起下面的笔,尝试搜索全名Zoey White-过滤器工作正常,直到您开始键入“White”。我假设代码中的某些内容没有选择一种允许您一次过滤多个数组的“AND”函数

有人对解决这个问题有什么建议吗

选项1:

向用户添加
fullName

$scope.users = [
    { firstName: "Camila", lastName: "Gilson", fullName: "Camila Gilson" },
    { firstName: "Zoey", lastName: "White", fullName: "Zoey White" },
];
备选案文2:

创建自定义搜索功能

HTML


  • {{user.firstName}{{user.lastName}}
角度控制

function UsersCtrl($scope) {
  // Defina query
  $scope.query = "";
  $scope.users = [
    { firstName: "Camila", lastName: "Gilson" },
    { firstName: "Zoey", lastName: "White" },
  ];

  // Custom search method
  $scope.search = function(user) {
    // Accept everything if query is empty
    if ($scope.query.length <= 0) return true;

    // Store value of query and name as lower case to make it kind of case insensitive
    var query = (""+$scope.query).toLowerCase(),
        fullName = [user.firstName, user.lastName].join(" ").toLowerCase();

    // Return true full name includes the query
    return fullName.indexOf(query) > -1;
  }
}
函数UsersCtrl($scope){
//定义查询
$scope.query=“”;
$scope.users=[
{姓:“卡米拉”,姓:“吉尔森”},
{姓:“佐伊”,姓:“怀特”},
];
//自定义搜索方法
$scope.search=函数(用户){
//如果查询为空,则接受所有内容
如果($scope.query.length-1;
}
}

类似,但用空格表示
而不是
:感谢@Blackhole,非常有趣的方法,但我选择了下面的答案,因为它更适合我的情况。再次感谢!完美!感谢aross。这正是我需要的。选择了选项2,因为它最适合这种情况。:)
function UsersCtrl($scope) {
  // Defina query
  $scope.query = "";
  $scope.users = [
    { firstName: "Camila", lastName: "Gilson" },
    { firstName: "Zoey", lastName: "White" },
  ];

  // Custom search method
  $scope.search = function(user) {
    // Accept everything if query is empty
    if ($scope.query.length <= 0) return true;

    // Store value of query and name as lower case to make it kind of case insensitive
    var query = (""+$scope.query).toLowerCase(),
        fullName = [user.firstName, user.lastName].join(" ").toLowerCase();

    // Return true full name includes the query
    return fullName.indexOf(query) > -1;
  }
}