【问题标题】:Angularjs custom filter that exactly work like normal in build filterAngularjs自定义过滤器在构建过滤器中完全正常工作
【发布时间】:2017-09-23 00:56:55
【问题描述】:

我在 jsfiddle 中有一个代码。我在 angularjs 的构建过滤器服务中使用 normal 的地方。但我需要一个自定义过滤器服务,它的行为与普通过滤器完全相同。我必须使用自定义过滤器,因为过滤后我必须做一些自定义操作。我的 jsfiddle 链接

https://jsfiddle.net/subhajitmaity/jzpmg1um/3/

请通过链接。

<div ng-app="app" ng-controller='AppCtrl'>
<input type="text" ng-model="search">
    <script type="text/ng-template" id="categoryTree">
        {{ category.title }}
        <ul ng-if="category.categories">
            <li ng-repeat="category in category.categories | filter:search" ng-include="'categoryTree'">           
            </li>
        </ul>
    </script>
    <ul>
        <li ng-repeat="category in categories | filter:search" ng-include="'categoryTree'"></li>
    </ul>    
</div>

【问题讨论】:

  • 你想要完成什么?
  • 我不想在构建过滤器中使用。而不是想要使用与构建过滤器中行为相同的自定义过滤器。

标签: angularjs angularjs-ng-repeat angularjs-filter


【解决方案1】:

您可以从这个自定义过滤器开始:

app.filter('myfilter', function() {
   return function( items, search) {
    var filtered = [];

    if(!search){
      return items;
    }

      angular.forEach(items, function(item){     
          if(item.title.toLowerCase().indexOf(search.toLowerCase()) !== -1){
             filtered.push(item);
          }
      })      

    return filtered;
  };
});

用法:

| myfilter:search

问题仍然存在:如果其中一个子过滤器包含 search 之一,则父过滤器应返回 true

【讨论】:

  • 它只过滤第一级。不能正常工作。如何解决问题?
  • @Rahul How to solve the problem? 没问题,纯代码写。发布您到目前为止所做的代码。您有足够的代码 sn-p 示例如何过滤嵌套数组。
  • 我把我的代码放在 plunker 里。请看上面的链接。无法解决的情况,这就是寻求帮助的原因
  • @Rahul 好吧,在您的演示中,您使用默认过滤器,并要求有人为您编写自定义过滤器和更适合嵌套的对象数组。我认为它超出了 StackOveflow 的目标。在这里,我们帮助修复现有代码,而不是从头开始编写代码。我向您发布了自定义过滤器的示例。使用它,它是一个好的开始。在自定义过滤器中编写您自己的逻辑,显示您卡在哪里。没有人愿意花时间为你写代码。
猜你喜欢
  • 2014-12-01
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
相关资源
最近更新 更多