angularjs - conditionally apply angualrjs custom filter in ng-repeat -


i want call custom filter based on input in template ng-repeat. if mycondition true want apply custom filter slice otherwise don't want apply slice filter.

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

above code not working slice filter getting true in input(5&&mycondition)

i can 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 not follow dry principle! have referred question @ making angular filter conditional can not use syntax filter:mycustomfilter in template need workaround.

help required.

if have full control of filter code, have parameter disable filter. not affect others, make last , optional parameter:

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

alternatively, if want follow example of disabling built-in filter, can make filter disabled if first (and only, in case) parameter undefined:

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

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -