gpt4 book ai didi

angularjs - Angular 过滤器名称作为变量

转载 作者:行者123 更新时间:2023-12-01 09:24:57 25 4
gpt4 key购买 nike

我正在设计从 ajax 读取数据和列的通用表。在列中,描述也是过滤器名称, Angular 应该用于特定列。但在 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 中执行此操作。首先,您需要在 Controller 中应用过滤器。

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)}}

关于angularjs - Angular 过滤器名称作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26710513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com