Angular10 isPlatformServer() Function
Last Updated :
06 Jan, 2023
Improve
In this article, we are going to see what is isPlatformServer in Angular 10 and how to use it.
The isPlatformServer is used to get a platform id that represents a server platform
Syntax:
isPlatformServer(platformId);
NgModule: Module used by isPlatformServer is:
- CommonModule
Return Value: returns a Boolean Value stating whether a platform id represents a server platform.
Approach:
- Create the angular app to be used
- Import isPlatformServer from @angular/core to the project.
- In app.component.ts define the object which holds the Boolean value.
- serve the angular app using ng serve to see the output
Example 1:
import { Component, Inject }
from '@angular/core';
import { PLATFORM_ID }
from '@angular/core';
import { isPlatformServer }
from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
isServer: boolean;
constructor( @Inject(PLATFORM_ID) platformId: Object) {
this.isServer = isPlatformServer(platformId);
console.log(this.isServer);
}
}
Output:
Example 2:
import { Component, Inject }
from '@angular/core';
import { PLATFORM_ID }
from '@angular/core';
import { isPlatformServer }
from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
isServer: boolean;
constructor( @Inject(PLATFORM_ID) platformId: Object) {
this.isServer = isPlatformServer(platformId);
}
}
<div *ngIf = 'isServer==false'>
platform id does not represents a server platform.
</div>
Output:
Reference: https://angular.io/api/common/isPlatformServer