web-dev-qa-db-ja.com

angular

Angularで、完全一致のみを返すようにフィルターを変更する方法はありますか?

例:

var words = [
    {   title: "ball"   },
    {   title: "wall"   },
    {   title: "all"    },
    {   title: "alloy"  }
];

var wordsFiltered = filter('filter')
( 
    words, 
    { 
        'title': 'all'
    } 
);  

上記は、「ボール」、「壁」、「すべて」、「合金」に一致します。しかし、私はそれが「すべて」にのみ一致することを望みます。それを変更する方法はありますか?

10
Ben

更新

AngularJS v.1.1.3以降、正確なフィルタリングは ネイティブで提供

Find words that exactly match title: 
<input ng-model="match.title" />
<br>
and exactly match type: 
<input ng-model="match.type" />
<hr>
<table>
  <tr ng-repeat="Word in words | filter:match:true">
   <td>{{Word.title}}</td> 
  </tr>
</table>

Plunker


あなたの質問は、複数のオブジェクトプロパティと照合したいことを示唆しているので、これを行うフィルターを次に示します。

app.controller('AppController',
    [
      '$scope',
      function($scope) {
        $scope.match = {};
        $scope.words = [
          { title: "ball", type: 'object' },
          { title: "wall", type: 'object' },
          { title: "all", type: 'Word' },
          { title: "alloy", type: 'material' }
        ];

      }
    ]
  );

app.filter('exact', function(){
  return function(items, match){
    var matching = [], matches, falsely = true;

    // Return the items unchanged if all filtering attributes are falsy
    angular.forEach(match, function(value, key){
      falsely = falsely && !value;
    });
    if(falsely){
      return items;
    }

    angular.forEach(items, function(item){ // e.g. { title: "ball" }
      matches = true;
      angular.forEach(match, function(value, key){ // e.g. 'all', 'title'
        if(!!value){ // do not compare if value is empty
          matches = matches && (item[key] === value);  
        }
      });
      if(matches){
        matching.Push(item);  
      }
    });
    return matching;
  }
});
<body ng-controller="AppController">

  Find words that exactly match title: 
  <input ng-model="match.title" />
  <br>
  and exactly match type: 
  <input ng-model="match.type" />
  <hr>
  <table>
    <tr ng-repeat="Word in words | exact:match">
     <td>{{Word.title}}</td> 
    </tr>
  </table>  
</body>

PLUNKER

19
Stewie

これを試して :

var words = [
    {   title: "ball"   },
    {   title: "wall"   },
    {   title: "all"    },
    {   title: "alloy"  }
];

var wordsFiltered = filter('filter')
( 
    words, 
    { 
        'title': 'all'
    },
    true
);
9
Jamal dine Y

正規表現を使用して、簡単な実装を実現できます。

<input ng-model="query" />
<tr ng-repeat="Word in words | filter: myFilter">

コントローラ内:

$scope.myFilter = function (Word) {
    if ($scope.query === '') return true;
    var reg = RegExp("^" + $scope.query + "$");
    return reg.test(Word.title);
};
3
zs2020

新しいフィルターを作成します。これは、あなたの望むことですか?

HTML

<div ng-controller="MyCtrl">
     {{words | exactMatch:'all'}} !
</div>

JavaScript

var myApp = angular.module('myApp',[]);

myApp.filter('exactMatch', function() {
    return function(words, pattern) {
        var result = [];
        words.forEach(function (Word) {
            if (Word.title === pattern) {
                result.Push(Word);
            }
        });                
        return result;
    }
});

function MyCtrl($scope) {
    $scope.words = [
        {title: "ball", other: 1},
        {title: "wall", other: 2},
        {title: "all", other: 3},
        {title: "alloy", other: 4},
        {title: "all", other: 5},
    ];
}

JsFiddle: jsfiddle
カスタムフィルターの詳細: フィルターカスタムフィルターの作成とフィルターの使用

HTMLの代わりにJavascriptでフィルターを使用したい場合は、ここを参照してください: jsfiddle

2
mchrobok

angular-filter はフィルターの便利なライブラリです。

それらのフィルターの1つは、完全に一致する where filter です。

0
Greg Quinn

独自のフィルターを作成しました。

<select 
        ng-model="selection.neType" 
        ng-options="option.NE_TYPE_ID as option.NAME for option in networkElementTypes">
            <option value="">Todos</option>
    </select>

<select 
        ng-model="selection.neTypeVendor" 
        ng-options="option.VENDOR_TYPE_ID as option.NAME for option in networkElementTypeVendors | exactMatch: {FK_NE_TYPE_ID: selection.neType}">
                <option value="">All</option>
            </select>

    app.filter('exactMatch', function() {
        return function(elements, pattern) {    

            var result = [];        
            var fieldSearch = Object.keys(pattern)[0];

            elements.forEach(function (element) {  

                if (element[fieldSearch] == pattern[fieldSearch]) {
                    result.Push(element);
                }
            });          

            return result;
        }
    });
0
Gabriel