Repository navigation
Closed
Labels
area: coreIssues related to the framework runtimeIssues related to the framework runtimecore: binding & interpolationIssue related to property/attribute binding or text interpolationIssue related to property/attribute binding or text interpolationfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issueshotlist: components teamRelated to Angular CDK or Angular MaterialRelated to Angular CDK or Angular Materialstate: has PR
Milestone
Description
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
Reacted by Brett Larson, Oskar Klintrot, Alexandru Sfirlogea, Reuben Nahouraii, Tim Kolotov, Martin Nyolt, Flux Xu, Yarn, Lasse Rosenow, youssefabidi and 137 more
Reacted by Serhii Stotskyi, Oskar Olsson and aceArt-GmbH
Reacted by keatkeat87, Luke Tanner, Dylan Hunn and aceArt-GmbH
Reacted by Amit Beckenstein and aceArt-GmbH
Metadata
Metadata
Assignees
Labels
area: coreIssues related to the framework runtimeIssues related to the framework runtimecore: binding & interpolationIssue related to property/attribute binding or text interpolationIssue related to property/attribute binding or text interpolationfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issueshotlist: components teamRelated to Angular CDK or Angular MaterialRelated to Angular CDK or Angular Materialstate: has PR
Activity
vicb commentedon Jan 27, 2017
Thought: we could support
x => {name: x}(without required "()") as we won't support fn body.gkalpak commentedon Jan 27, 2017
In AngularJS, we used to recommend against adding too much logic in the templates. For example:
Is this less of a concern in Angular now?
I know this is meant for good and being able to
(a, b) => a + borx => x.someMethod()can come very handy (and doesn't include too much logic), but I am afraid people will end up doing things more like:jelbourn commentedon Jan 27, 2017
@gkalpak My opinion on this one is that while we do want to discourage complex logic in the template, ultimately the template syntax exists in the first place to be a convenience and improve developer productivity. This feature makes it simple and clean to capture a somewhat common use-case, especially for functional-style programming, which is worth the fact that some people may go overboard with it.
toverux commentedon Oct 28, 2017
Found the issue because I needed it to have thunks in my templates.
So I think there are valid use cases (mine may seem strange but I can assure you that it makes sense with the thing I'm working on). But I agree with @gkalpak about potential abuses. Personally, I've not been surprised when the JiT compiler blown up when I tried to use an arrow function.
But still, that would be a nice and neat feature to have for responsible developers.
matheo commentedon Dec 8, 2017
As a related issue, in Material 2 Autocompleter, the
displayWithfunction is not binded to the Component, and I was not able to access the internal variables that I require to figure out the result.I figured out a workaround that worked, to keep my logic in the Component:
then "inject" it into the template:
Without wrap the arrow function,
this.langis undefined :(mtraynham commentedon Dec 21, 2017
@matheo you can use
.bind(this):component.ts
component.html
thisis in reference to the component instance.Still would like to see this inlined arrow feature though.
antontrafimovich commentedon Oct 26, 2018
Is there any update?
frankiDotNet commentedon Jan 29, 2019
Is there any way to make a workaround for this? Something like a Pipe that supports multiple parameters and returns a function?
Guatom commentedon Feb 26, 2019
@Franki1986: as far as I could tell, there are only two ways to get this:
equals = (a: Country, b: Country) =>and notfunction equals(a: Country, b: Country) {or you'll losethis.thiswithbind, just like @mtraynham said. But this feels so 2014...gotwig commentedon Mar 7, 2019
Pretty please 👍 🦄 especialy the displayWith usecase sucks 🤢 Maybe Ivy can handle this?
21 remaining items
BestOfTheBestSir commentedon Aug 9, 2023
I really hope it will never be implemented. It will lead to such an awful code which I usually see just in the JS portion, but it will also lead to performance degradation, which will be visible as a framework issue, while it will be an issue of a developer.
jjamid commentedon Aug 9, 2023
I don't agree. Commenting on problematic code is always relvant, even if it's not related, since it's public. You cannot "avoid errors"! You're just moving the problem to the next much harder to troubleshoot step. The "?" is responsible personally for thousands of bugs that I've seen in the developers' code (and it usually makes the code extremly hard to understand when used wrong), it's because it's the exact opposite of the Guard Clause idea.
Anyway, I think too that there should be arrow support in the angular html. And not only that, also access to Enums without workarounds and basically to be excatly like .NET's amazing razor files and Web Forms before that. Eventually the html will inherit the component, it's just a matter of time. They now allow accessing protected memebers in the html.
Everything can be abused (like the "?" operator) and it's not a reason to not implement it.
LeonardoX77 commentedon Aug 9, 2023
welcome to the pros list and don't worry about my awful code :), probably you will never have to deal with it.
StephenSPaul commentedon May 18, 2025
I'd like to make some arguments for this:
ng-templateandngTemplateOutletwhich are a bit obscure to most devs, and have unresolved issues around type-safety.Syntax proposal:
mauriziocescon commentedon May 21, 2025
Hey!
I know it's gonna be a bit of a bold statement😅... but as part of a potential new authoring format, I personally would like to use arrow-func for events as well. Basically something like:
Some reasoning:
binding arrow functions for events (typically in the form
{ e => method(e) }) is basically industry standard: solid, react, svelte, vue (albeit with lots of options), lit, ... I think it might be time to align (less things to learn or remember).I assume we're all used to such notation, but IMHO it's anyway a bit strange:
[anInput]="..."we expect the binding to apply immediately (pass / update the value),(anEvent)="method()"should call the method right away... rather than bind the func as an event handler. 😅I've always dismissed "as naive" the frustration on this point among the colleagues having a react background. But now that I've spent the last month working with svelte, I get the point. 😅
Just an idea for the future.
Cheers!
feat(core): support arrow functions in expressions
feat(core): support arrow functions in expressions
feat(core): support arrow functions in expressions
feat(core): support arrow functions in expressions
feat(core): support arrow functions in expressions
angular-automatic-lock-bot commentedon Feb 9, 2026
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.