Carlos Cifuentes
AngularJS → Angular reference

filters in modern Angular

Filters become pipes. The template syntax is the same; the definition is a class with a transform method.

Before · AngularJS
app.filter('initials', function () {
  return function (name) {
    return name.split(' ').map(function (p) { return p[0]; }).join('');
  };
});

{{ user.name | initials }}
After · Angular
@Pipe({ name: 'initials' })
export class InitialsPipe implements PipeTransform {
  transform(name: string): string {
    return name.split(' ').map(p => p[0]).join('');
  }
}

{{ user.name | initials }}

What changes

  • Built-in date, currency, number, uppercase and json filters have direct pipe equivalents.
  • The filter and orderBy filters do not exist: filter and sort in the component.
  • Pipes are pure by default, so they only run when the input changes.

Migration tip. Date and number formats differ slightly between AngularJS filters and Angular pipes. Compare the output on real data before releasing.

Related

Planning a migration or a micro-frontend platform?

In a 20-minute call you describe your situation and I tell you how I'd approach it. If it isn't worth doing yet, I'll say so.

Message me on LinkedIn