AngularJS : filter and bold part of the result

后端 未结 2 2062
傲寒
傲寒 2021-02-08 13:14

I have a list filtered like this:

ng-repeat=\"item in items | filter:query | limitTo:10\"

and a search input

ng-model=\"search.         


        
2条回答
  •  囚心锁ツ
    2021-02-08 13:38

    Two hints for the answer from gion_13.

    If the query is a empty string (after filtering and then deleting the search term), then the input "apple" is modified like this: apple

    The solution for this is to change either the regex or a early return:

    .filter('searchfilter', function() {
        return function (input, query) {
            if (query === '') {
                return input;
            }
            return input.replace(RegExp('('+ query + ')', 'g'), '$1');           
        }    
    });
    

    If you don't want a case sensitive filter than change the RegExp:

    RegExp('('+ query + ')', 'gi'), '$1');           
    

提交回复
热议问题