1

I know one can use AngularJS filters in templates: {{ someData | someFilter }} as well as in Controller (see this). Using filters in templates is natural and more readable, especially when dealing with big apps.

My question is: why would one prefer to call a filter from a controller rather than from a template? When does it make sense? And what are the pros & cons of both?

Community
  • 1
  • 1
ducin
  • 25,621
  • 41
  • 157
  • 256
  • 2
    Major difference is the way digest cycles work. That same filter in controller may only be called once whereas when used in view can be called many many times. No steadfast rule as it depends on view structure as well as what is being filtered. Understanding digest cycles is critical to understanding the differences and seeing how performance will be lost with extra work in each digest – charlietfl Jun 05 '16 at 11:58
  • 1
    Another reason is that you don't always **display** something. Sometimes you do something else with things, like compute results, send something to a server, etc. Let's say I need to sort an array of people by first name and last name before sending them to some API: using the orderBy filter in the view won't really be possible. I need to use it in the controller (or service) code. – JB Nizet Jun 05 '16 at 12:53

0 Answers0