How to get localStorage value in Angular 2 template

When you are trying to use *ngIf="localStorage.getItem(comment._Id) == 'liked'" it's trying to find this.localStorage in component.ts, but it doesn't exist, so it's throwing an error... Things like localStorage is common to use wherever required so keep it in global.ts which can be accessed easily...

In your global.ts, add a function like this:

export class GlobalApp {

constructor() {}

public localStorageItem(id: string): string {
    return localStorage.getItem(id);
}

Now update your component.ts:

import {GlobalApp} from '../helpers';

export class MyComponent {

constructor (public app: GlobalApp)  {
  }
}

Now it's ready to easily use in any template as we have a global declared function:

*ngIf="app.localStorageItem(comment._Id) == 'liked'"