AngularJS使用一个字段搜索多个关键字

AngularJS使用一个字段搜索多个关键字,angularjs,search,angularjs-ng-repeat,angular-ngmodel,angular-filters,Angularjs,Search,Angularjs Ng Repeat,Angular Ngmodel,Angular Filters,我对AngularJS是新手。我有3个输入字段,最多可为一篇文章输入3个关键字: <input type="text" ng-model="Keyword1"></input> <input type="text" ng-model="Keyword2"></input> <input type="text" ng-model="Keyword3"></input> 并通过以下方式进行搜索: Search Keyword:

我对AngularJS是新手。我有3个输入字段,最多可为一篇文章输入3个关键字:

<input type="text" ng-model="Keyword1"></input>
<input type="text" ng-model="Keyword2"></input>
<input type="text" ng-model="Keyword3"></input>
并通过以下方式进行搜索:

Search Keyword: <input ng-model="searchKeyword.Keyword1">
搜索关键字:
如您所见,这目前只搜索每篇文章的第一个关键字。我怎么能只有一个搜索字段来搜索一篇文章的所有三个关键字?我知道我不能这么做

<input ng-model="searchKeyword.Keyword1.Keyword2.Keyword3">


谢谢

以下是我将要做的

  • 编写自定义筛选器()
  • 在过滤器中,按空格分割输入(这将为您提供单独的单词)
  • 逐个搜索输入中的所有单词

  • 不确定目的是返回与至少一个关键字匹配的所有帖子,还是返回与所有关键字匹配的所有帖子。以下是如何创建自定义筛选器以匹配至少一个单词:

    <div ng-controller="theController">
      <input ng-model="inputText"/>
      <!-- Use a custom filter to determine which posts should be visible -->
      <ul>
        <li ng-repeat="post in posts | filter:myFilter">{{post}}</li>
      </ul>
    </div>
    
    angular.module('theApp', [])
      .controller('theController', ['$scope', function($scope) {
        $scope.inputText = 'test';
        $scope.posts = [
          'test test2;lksdf asdf one asdf poo',
          'arm test test2 asdf',
          'test head arm chest'
        ];
    
        $scope.myFilter = function(post) {
          // default to no match
          var isMatch = false;
    
          if ($scope.inputText) {
            // split the input by space
            var parts = $scope.inputText.split(' ');
    
            // iterate each of the words that was entered
            parts.forEach(function(part) {
              // if the word is found in the post, a set the flag to return it.
              if (new RegExp(part).test(post)) {
                isMatch = true;
              }
            });
          } else {
            // if nothing is entered, return all posts
            isMatch = true;
          }
    
          return isMatch;
        };
      }]);
    
    
    
    • {{post}
    angular.module('theApp',[]) .controller('theController',['$scope',function($scope){ $scope.inputText='test'; $scope.posts=[ “测试2;lksdf asdf一个asdf poo”, “arm测试2 asdf”, “测试头-手臂-胸部” ]; $scope.myFilter=函数(post){ //默认为不匹配 var isMatch=错误; if($scope.inputText){ //按空格分割输入 变量部分=$scope.inputText.split(“”); //重复输入的每个单词 部分。forEach(功能(部分){ //如果在帖子中找到了该单词,a将设置标志以返回该单词。 if(新RegExp(部分).测试(post)){ isMatch=true; } }); }否则{ //如果未输入任何内容,则返回所有帖子 isMatch=true; } 返回isMatch; }; }]);
    这里是plunkr:


    注意:此解决方案不会将关键字限制为3。可以输入任意数量的关键字,按空格字符分隔。如果未输入任何内容(或仅输入空格),则返回所有内容。

    当它是一个键对象时,您该怎么办?
    <div ng-controller="theController">
      <input ng-model="inputText"/>
      <!-- Use a custom filter to determine which posts should be visible -->
      <ul>
        <li ng-repeat="post in posts | filter:myFilter">{{post}}</li>
      </ul>
    </div>
    
    angular.module('theApp', [])
      .controller('theController', ['$scope', function($scope) {
        $scope.inputText = 'test';
        $scope.posts = [
          'test test2;lksdf asdf one asdf poo',
          'arm test test2 asdf',
          'test head arm chest'
        ];
    
        $scope.myFilter = function(post) {
          // default to no match
          var isMatch = false;
    
          if ($scope.inputText) {
            // split the input by space
            var parts = $scope.inputText.split(' ');
    
            // iterate each of the words that was entered
            parts.forEach(function(part) {
              // if the word is found in the post, a set the flag to return it.
              if (new RegExp(part).test(post)) {
                isMatch = true;
              }
            });
          } else {
            // if nothing is entered, return all posts
            isMatch = true;
          }
    
          return isMatch;
        };
      }]);