Skip to content

Angular, Safe navigation operator.

2017-11-13 05:00:17

Simple example of Safe navigation operator. Everything what you need will be generated after executing this command ng new hello-word. After that, what you have to do, is adjust few files. Of course more you can find in Angular documentation, here we have only my notes. One more thing, if you don't like to install node mess on your local computer you can use my or similar docker image

The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths. Here it is, protecting against a view render failure if the currentHero is null. mode

Old way (app.component.html)

<pre *ngif='"person.addressHome"'>{{}}</pre>

New way (app.component.html)



import {Component} from '@angular/core';

  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent {
  title = 'app';

  person = {
    name: "Michał",
    addressWork: {
      city: "Gdańsk"
    // addressHome: {
    //   city: "Toruń"
    // }