我想根据我的模板中的输入使用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'">更多推荐
发布评论