question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nullish coalescing operator cannot be used in two-way binding

See original GitHub issue

Bug Report

Affected Package

Is this a regression?

No, this is a new feature.

Description

Nullish coalescing operator support that was recently added in Angular 12 breaks on compilation when used inside sandwich style syntax [(ngModel)]

Minimal Reproduction

https://stackblitz.com/edit/angular-ivy-phyfc4?file=src/app/app.component.ts

<app-test-component [(value)]="test ?? 0"></app-test-component>

Exception or Error

error NG5002: Parser Error: Unexpected token '=' at column 10 in [test ?? 0=$event] 

Your Environment

Angular Version:

Angular CLI: 12.0.3
Node: 16.3.0
Package Manager: npm 7.15.1
OS: win32 x64

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:20
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
petebacondarwincommented, Aug 20, 2021

We discussed this feature request today. While it is true that we could create some additional logic that enabled two-way binding to handle this scenario, it would add a new rules to our expression syntax that would move it away from how JavaScript works. We are actually trying to move in the opposite direction - to make the expression language closer to vanilla JavaScript.

Currently the rule is simple and aligned with JavaScript: the “banana-in-a-box” syntax [(prop)]="expression" gets “desugared” to a pair of expressions, the input [prop]="expression" and the output (propChange)="expression=$event".

It is clear when desugared that the output expression requires the expression to be a “assignable”. In JavaScript, nullish coalescing expressions are not assignable (e.g. value ?? 0 = $event) along with a number of other scenarios such as optional chaining (value?.prop = $event), ternary ((condition ? value1 : value2) = $event), function calls (foo(value) = $event) and so on.

Therefore we don’t intend to implement this feature in Angular.

One way to workaround this limitation (both in JavaScript and the Angular expression syntax) is to use getters and setters. For example, rather than putting the nullish coalescing in the template you put it in the getter for a property:

class MyComponent {
  private _value: number|undefined;
  get value(): number { return this._value ?? 42; }
  set value(v: number): number { this._value = v; }
}

The the value property can be referenced in a two-way binding in the component’s template:

<input [(ngModel)]="value">
3reactions
dario-piotrowiczcommented, Jun 4, 2021

I may be wrong but to me this sort of things seem to go against what the two way data binding is about, if you assign something dynamic and not a field of your component then the semantic of the operation gets quite unclear, doesn’t it?

On a similar note I just wanted to also point out that the ternary operator has a similar issue with the two way data binding ( as you can see here for example: https://medium.com/programming-beasts/ternary-operator-in-angular-two-way-binding-1da312916779 )

Read more comments on GitHub >

github_iconTop Results From Across the Web

NG8102: Nullish coalescing not nullable - Angular
This diagnostic detects a useless nullish coalescing operator ( ?? ) characters in Angular templates. Specifically, it looks for operations where the input...
Read more >
Javascript nullish coalescing chained with "or" - Stack Overflow
According to the documentation, you can't chain the nullish coalescing operator with AND or OR: MDN: It is not possible to combine both...
Read more >
What's new in Angular 13.2? - Ninja Squad
nullishCoalescingNotNullable. The first one logs a warning (code NG8101) if you write a two-way binding ngModel with ([ngModel)] instead of ...
Read more >
Code Angular the 2022 Way - JavaScript in Plain English
Have you used nullish coalescing operators on variables that are not nullable or not undefined? Although it is a good practice to use...
Read more >
Design Patterns for Building Reusable Svelte Components
For that, we can use Svelte's two-way binding. ... use the nullish coalescing operator to convert `null` to a number const value: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found