过滤AngularJS中的多个属性

时间:2017-01-04 17:06:44

标签: javascript angularjs loops angularjs-scope angularjs-ng-repeat

我正在尝试过滤Angular中的多个属性。我可以使用filter:{ title: searchString }轻松过滤一个属性,但是当我尝试使用多个属性时......它似乎根本没有得到任何结果。

我需要做的是:

如果John Wayne通过..它会显示John Wayne,但如果仅John或仅Wayne通过...它仍会显示John Wayne 。同样适用于n w,因为Johnn结尾,subTitlew开头

我的代码:

<input type="text" ng-model="searchString">

<div ng-repeat="arr in arr1 | filter:{ title: searchString, subTitle: searchString }"></div>

$scope.arr1 = [
    {title: 'John',subTitle:'Wayne'}
    {title: 'Barry'}
];

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var app = angular.module("app", []);

app.controller("ListCtrl", ["$scope",
  function($scope) {
$scope.arr1 = [
    {title: 'John',subTitle:'Wayne'},
    {title: 'Barry'}
];
  }
 
]);
&#13;
<!DOCTYPE html>
<html>
<head>
  <script data-require="angular.js@1.4.7" data-semver="1.4.7" src="https://code.angularjs.org/1.4.7/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>
<body ng-app='app'>
  <div ng-controller="ListCtrl">
   title:<input type="text" ng-model="criteria.title"> 
   subtitle:<input type="text" ng-model="criteria.subTitle"> <br>
    <div ng-repeat="arr in arr1 |  filter: criteria">{{arr}}</div>
  </div>
</body>
</html>
&#13;
&#13;
&#13;