Why benefits or filter and orderBy?

It seems like AngularJS really pays a lot of attention to using filters and another ng directive in your view to filter and sort your data instead of doing it manually in the model. Is there a reason for this, i.e. Faster, caches or something else?

I want to show a sorted list, for example, but I also want to access the sorted list for other purposes that are not related to viewing. It is very simple if the list is sorted directly in the model, so I'm trying to figure out if there is a flaw in this.

Thanks!

+7
source share
1 answer

I don't see anything wrong with pre-sorting the data, if that makes sense to you, but here are some pros and cons to using Angular filters.

Pros:

  • A clear separation of representation and model. The model / controller does not need to know or include code related to how the data will be displayed / sorted / filtered
  • Since filters are executed as the model changes, orderBy filter can be automatically sorted as elements are added to the array through the user interface
  • Filters can be used to format data for display (for example, a filter filter ) without changing the data of the base model
  • Facilitates the reuse of commonly used built-in functions or custom filter functions.

Minuses:

  • A poorly written filter function can cause performance problems. You can see a specially contrived example in the AngularJS Batarang video starting at 4:30. Any code (not just a filter) can be poorly written, but it is not clear from the beginning how often the filter is called.
  • It’s a bit confusing that some filters act on one number / line (currency filter), and some on arrays (orderBy filter)
  • The syntax for passing arguments and a filter chain can also be a bit confusing

I'm sure there are many more pros and cons, but hopefully this helps!

+10
source

All Articles