【问题标题】:dynamic filters in Angularjs with error checking includedAngularjs中的动态过滤器,包括错误检查
【发布时间】:2015-07-23 15:05:01
【问题描述】:

我想应用这样的动态过滤器,但还要检查过滤器是否存在以处理任何错误。

<td ng-repeat="row in table.row>
  {{ row[ column.row.dataKey ] | column.row.dynamicFilterName }}

【问题讨论】:

    标签: javascript angularjs dynamic service filter


    【解决方案1】:

    这是我经过一番研究后得出的结论,希望对您有所帮助...

    注意: column.row.filter === 'myFilterName'; // 没有 $ 继续字符串。

    <td ng-repeat="row in table.row>
      {{ row[ column.row.dataKey ] | applyFilter: column.row.filter }}
    </td>
    

    注意: applyFilter 是一个实际的过滤器,它会应用其他过滤器。在 Stack 上找到了这个解决方案:dynamic filtering

    app.filter( 'applyFilter', [ '$filter', '$injector', function( $filter, $injector ){
    
      var filterError = "The following filter does not exist: ";
    
      return function ( value, filterName ){
    
       // Just return the value if no filtername provided.
        if( !filterName ){ return value; }
    
        // Below checks the filter name in the injector as "filterNameFilter"
        // then spits out a soft error and just returns the value if 
        // no filter exists.
    
        if( !$injector.has( filterName + "Filter" )){ console.error( filterError + filterName ); return value; }
    
        // if there is a string name that exists as a filter, then apply it.
        return $filter( filterName )( value );
      };
    }]);
    

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 2019-04-09
      • 2014-05-03
      • 2017-06-08
      • 2014-06-09
      • 1970-01-01
      相关资源
      最近更新 更多