How to check for isBrowser in Angular 4

Solution 1:

import { PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser} from '@angular/common';
...
export class MyComponent {
...
    testBrowser: boolean;
    constructor(
        @Inject(PLATFORM_ID) platformId: string) {
            this.testBrowser = isPlatformBrowser(platformId);
            if (this.testBrowser) {
                //this is only executed on the browser
            }
    }
...

Solution 2:

You can import isPlatformBrowser(<platform id>) as so:

import { isPlatformBrowser } from '@angular/common';

and that will allow you to check for whether it is rendering in browser or not.

As a note, there is also a isPlatformServer in @angular/common as well.