Why and When to Use Filters

Filters let you define commonly used data transformations so that they can be applied throughout an application, without being tied to a specific controller or type of data. Filters transform the data as it passes from the scope to the directive but don’t change the source data, allowing you to flexibly format or transform data for display in views.

You could include the transformation logic in your controller behaviors or in custom directives, but separating transformation into reusable filters increases the flexibility of the application by allowing you to apply different filters to the same behaviors and directives and display data in different ways, either within the same view or in different views. Table 14-2 summarizes why and when to use filters in an AngularJS application.

Source: Freeman Adam (2014), Pro AngularJS (Expert’s Voice in Web Development), Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *