exact filter in angular

前端 未结 6 1365
天命终不由人
天命终不由人 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:42

    You can use Regex to achieve a simple implementation:

    
    
    

    In the controller:

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

提交回复
热议问题