Filtering nested objects in ng-repeat with a search input field

后端 未结 2 1565
难免孤独
难免孤独 2020-12-10 19:00

I am trying to filter nested objects in ng-repeat by using a search textbox.

Given the following object:

$scope.items = {
    \"1\": {
        name:          


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 19:07

    I've finally got the answer to my own question.

    I only had to create my own filter and check if the properties inside the object have the desired value by using regular expressions:

    app.filter('customSearchFilter', function() {
    return function(input, term) {
        var regex = new RegExp(term || '', 'i');
        var obj = {};
        angular.forEach(input, function(v, i){
          if(regex.test(v.name + '')){
            obj[i]=v;
          }
        });
        return obj;
      };
    });
    

    And apply it in the HTML this way:

    
    
    • Both {{key}} and {{val.name}}

    I created this Plunker to show my solution in action

提交回复
热议问题