angular: [4.0.0-rc.4] isPlatformBrowser(PLATFORM_ID) not returning correct platform

I’m submitting a … (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

PLATFORM_ID taken from @angular/core returns this object (logged):

InjectionToken {
  _desc: "Platform ID"
}

while it is validated against string value “browser” (from ɵPLATFORM_BROWSER_ID) using isPlatformBrowser(PLATFORM_ID).

Thus, isPlatformBrowser(PLATFORM_ID) never returns true.

Expected behavior isPlatformBrowser should return true on browser platform. Same holds for isPlatformServer(PLATFORM_ID).

Minimal reproduction of the problem with instructions

import { PLATFORM_ID } from '@angular/core';
console.log(PLATFORM_ID); // prints object

import { isPlatformBrowser, isPlatformServer, ɵPLATFORM_BROWSER_ID } from '@angular/common';

console.log(ɵPLATFORM_BROWSER_ID); // returns 'browser'
console.log(isPlatformBrowser(PLATFORM_ID)); // returns false anyway

What is the motivation / use case for changing the behavior?

Should be usable to distinguish Universal server app from browser app.

Please tell us about your environment:

  • VSCode
  • NPM
  • Angular version: 4.0.0-rc.4
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

all

  • Language: [all | TypeScript X.X | ES6/7 | ES5] TypeScript

  • Node (for AoT issues): node --version = v7.7.0

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

import { PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser, isPlatformServer } from '@angular/common';

constructor(@Inject(PLATFORM_ID) platformId: string) {
   isPlatformBrowser(platformId);
}

console.log(ɵPLATFORM_BROWSER_ID)

and don’t use things with ɵ prefix. it’s a private api and it can change any time

When I try to inject platformId I receive this error Error: No provider for PLATFORM_ID!