The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Extract predicate methods into filters for ng-if and ng-show

Extract predicate methods into filters for ng-if and ng-show

3:09
Leaking logic in controllers is not an option, filters are a way to refactor your code and are compatible with ng-if and ng-show
Watch this lesson now
Avatar
egghead.io

Leaking logic in controllers is not an option, filters are a way to refactor your code and are compatible with ng-if and ng-show

Avatar
Adrien

Good Job, a good filter example :-) !

Compared to your previous lesson about caching, does angular caches filters that accept arguments.?

I was also wondering if it is possible to handle filter caching problems more cleanly using filter arguments?

In reply to egghead.io
Avatar
Benjamin

I've not heard about angular builtin mechanism for cache. Do you have a source?

I think using a filter or a function should have the same effect in terms of performance. I tend to prefer filters for reusability.

If you use memoize provided by lodash/underscore, you have a cache depending on arguments. Not sure I'd advise this, because like always with caching, its tough to decide whenever you need to invalidate cached data.

In reply to Adrien
Avatar
Adrien

Ok, I think my question was not clear. It was related to your previous lesson about state-full filters.

I often need to implement filters that are initialized using promises. I currently use state-full filters but I would prefer using state-less filters for performance reason (because angular does caching for us).

I haven't found yet a clean way to do that. Do you have an idea?

In reply to Benjamin
Avatar
Benjamin

Why do you say angular caches for you? Where did you read that?

What you describe was possible till angular 1.2.x see https://jsbin.com/lugiko/edit?html,js,output

but try to change the version in the js bin, it now fails (for optimization reasons). So stateful filters are the only way I know

In reply to Adrien
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?