Search code examples
angularsafe-navigation-operator

Where should I use Safe Navigation (Elvis Operator, the "?") in Angular?


We undoubtedly use safe navigation in string interpolation ({{}}) to safely navigate values in a nested object with checking nulls or undefined. I have a question that, is it okay to use safe navigation also for:

  1. ngIf
  2. ngFor
  3. ngModel
  4. ngClass
  5. and other html property bindings like [disabled], [style], etc.

For which scenarios should I use and not?


Solution

  • We can use ? in every place you have mentioned, except two way binding ngModel. You could use when you are using [ngModel] but not when using [(ngModel)]