有条件地在ng

系统教程 行业动态 更新时间:2024-06-14 16:59:47
有条件地在ng-repeat中应用angualrjs自定义过滤器(conditionally apply angualrjs custom filter in ng-repeat)

我想根据我的模板中的输入使用ng-repeat调用自定义过滤器。 所以如果myCondition为真,那么我只想应用我的自定义滤镜片,否则我不想应用切片滤镜。

<div ng-repeat = "job in userjobs.matched_jobs | slice:'5' && myCondition"> <div ng-include="'applydiv.html'"></div> </div>

上面的代码不起作用,因为切片过滤器在输入中变为真(5 && myCondition)

我可以在这里破解

<div ng-if="!myCondition"> <div ng-repeat = "job in userjobs.matched_jobs"> <div ng-include="'applydiv.html'"></div> </div> </div> <div ng-else> <div ng-repeat = "job in userjobs.matched_jobs | slice:'5'"> <div ng-include="'applydiv.html'"></div> </div> </div>

但是它不会遵循DRY原则! 我已经提到了使角度过滤器有条件的问题,但我不能在模板中使用语法过滤器:myCustomFilter,所以我需要另一个解决方法。

需要帮助。

I want to call custom filter based on input in my template with ng-repeat. so if myCondition is true then only I want to apply my custom filter slice otherwise I don't want to apply slice filter.

<div ng-repeat = "job in userjobs.matched_jobs | slice:'5' && myCondition"> <div ng-include="'applydiv.html'"></div> </div>

above code is not working as slice filter is getting true in input(5&&myCondition)

I can do hack here that

<div ng-if="!myCondition"> <div ng-repeat = "job in userjobs.matched_jobs"> <div ng-include="'applydiv.html'"></div> </div> </div> <div ng-else> <div ng-repeat = "job in userjobs.matched_jobs | slice:'5'"> <div ng-include="'applydiv.html'"></div> </div> </div>

But then it will not follow DRY principle! I have referred question at Making an angular filter conditional but I can not use syntax filter:myCustomFilter in template so I need some another workaround.

Help required.

最满意答案

如果您完全控制过滤器代码,则始终可以使用参数来有效禁用过滤器。 不要影响他人,将其作为最后一个和可选参数:

app.filter("slice", function(){ return function(input, number, disable){ if (disable) return input; // the rest of your filter } }) <div ng-repeat="item in items | slice:'5':!myCondition">

或者,如果您想要遵循禁用内置过滤器的示例 ,您可以使它在禁用过滤器时如果第一个(仅在此情况下)参数undefined :

app.filter("slice", function(){ return function(input, number){ if (!angular.isDefined(number)) return input; // the rest of your filter } }) <div ng-repeat="item in items | slice: (!myCondition || undefined) && '5'">

If you have full control of the filter code, you could always have a parameter to effectively disable the filter. Not to affect others, make it the last and optional parameter:

app.filter("slice", function(){ return function(input, number, disable){ if (disable) return input; // the rest of your filter } }) <div ng-repeat="item in items | slice:'5':!myCondition">

Alternatively, if you want to follow the example of disabling the built-in filter, you can make it so the filter is disabled if the first (and only, in this case) parameter is undefined:

app.filter("slice", function(){ return function(input, number){ if (!angular.isDefined(number)) return input; // the rest of your filter } }) <div ng-repeat="item in items | slice: (!myCondition || undefined) && '5'">

更多推荐

本文发布于:2023-04-17 09:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/24f6d5ae8e31b56a16674d590a80b5be.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   ng

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!