角度过滤器名称作为变量 [英] angular filter name as variable

查看:137
本文介绍了角度过滤器名称作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计从ajax读取数据和列的通用表。
在列中描述也是角度应该用于特定列的过滤器名称。
但是,在HTML模板中,我不能使用过滤器名称的变量:/
是否有解决方案?或者,我应该与数据源代码的JavaScript循环?



这是代码示例:

 < tr ng-repeat =item in data> 
< td ng-repeat =col in cols>
{{item [col.source]}}
< span ng-if =col.ngFilter>
{{col.ngFilter}} //前状态过滤器
{{项目[col.source] | col.ngFilter}} //不工作 - 正在寻找col.ngFilter而不是状态过滤器。
< / span>
< / td>
< / tr>


解决方案

首先,你需要在你的控制器中应用这个过滤器。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ scope.applyFilter = function(model,filter){
return $ filter(filter)(model);
};

$ b

然后,在你的HTML中:



而不是

  {{item [col.source] | col.ngFilter}} 

使用

   


I'm designing universal table that reads data and columns from ajax. In columns description is also filter name which angular should use for a specific column. But in HTML templates I can't use variables for filter names:/ Is there a solution for that? Or should I code javascript loop with data source?

Here is code example:

<tr ng-repeat="item in data">
    <td ng-repeat="col in cols">
        {{item[col.source]}}
        <span ng-if="col.ngFilter">
            {{col.ngFilter}} // ex. "state" filter
            {{item[col.source]|col.ngFilter}} //is not working - is looking for "col.ngFilter" not "state" filter.
        </span>
    </td>
</tr>

解决方案

You cannot do it in your HTML. First, you need to apply the filter in your controller.

function MyCtrl($scope, $filter) {

    $scope.applyFilter = function(model, filter) {
        return $filter(filter)(model);
    };

}

Then, in your HTML:

Instead of

{{item[col.source]|col.ngFilter}}

use

{{applyFilter(item[col.source], col.ngFilter)}}

这篇关于角度过滤器名称作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆