exact filter in angular

前端 未结 6 1360
天命终不由人
天命终不由人 2020-11-29 12:10

In Angular, is there a way to modify the filter such that it only returns exact matches?

Example:

var words = [
    {   title: \"ball\"   },
    {            


        
6条回答
  •  离开以前
    2020-11-29 12:38

    I would create a new filter. Is this what you want?

    HTML

    {{words | exactMatch:'all'}} !

    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
    More information about custom filters: filters, creating custom filters and using filters

    If you want use filter in Javascript instead of html you should look here: jsfiddle

提交回复
热议问题