Skip to content

Support arrow functions in template syntax #14129

@jelbourn

Description

@jelbourn

It's not uncommon for a component to take as input a transformation function or predicate function.

For example, this PR for @angular/forms adds an @Input for a predicate function for determining if two options in a <select> are equal. Right now, this looks something like:

<select [compareWith]="equals" [(ngModel)]="selectedCountries"> ...

equals = (a: Country, b: Country) => {
   return a.id === b.id;
};

With this feature, this could be written as

<select [compareWith]="(a, b) => a.id == b.id" [(ngModel)]="selectedCountries"> ...

There are other places in Angular Material where we're looking to add similar API, e.g., datepicker with something like:

<md-datepicker [allowedDateFilter]="d => isWeekend(d)">

Or autocomplete, where the component needs to take an arbitrary value and know what string to write into the text input:

<md-autocomplete [displayWith]="value => value.getFullName()">

This would have to account for:

  • Functions with multiple arguments ((a, b) => a + b)
  • Functions with param names that already exist in the context (<my-x #x [uniq]="x => x.id">)
  • Functions that return an object literal (x => ({name: x}))
  • Invoking functions on the context (x => isActive(x)
  • Passing functions through a pipe (x => x.activate() | debounce)

FYI @mhevery for planning

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimecore: binding & interpolationIssue related to property/attribute binding or text interpolationfeatureIssue that requests a new featurehotlist: components teamRelated to Angular CDK or Angular Material

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions