加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 角度过滤器名称作为变量

发布时间:2020-12-17 07:08:21 所属栏目:安全 来源:网络整理
导读:我正在设计从ajax读取数据和列的通用表. 在列中,描述也是过滤器名称,其角度应该用于特定列. 但在 HTML模板中我不能使用变量作为过滤器名称:/ 有解决方案吗?或者我应该使用数据源编写 javascript循环? 这是代码示例: tr ng-repeat="item in data" td ng-r
我正在设计从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}} // ex. "state" filter
            {{item[col.source]|col.ngFilter}} //is not working - is looking for "col.ngFilter" not "state" filter.
        </span>
    </td>
</tr>

解决方法

您无法在HTML中执行此操作.首先,您需要在控制器中应用过滤器.

function MyCtrl($scope,$filter) {

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

}

然后,在您的HTML中:

代替

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

使用

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读