How to use ngSwitch on datatype in angular? How to use ngSwitch on datatype in angular? typescript typescript

How to use ngSwitch on datatype in angular?


An alternative would be to use ngIf:

  <p *ngIf="isObject(value)">This is Object</p>  <p *ngIf="isArray(value)">This is Array</p>  <p *ngIf="isBoolean(value)">This is Boolean</p>  <p *ngIf="isNumber(value)">This is Number</p>  <p *ngIf="!isObject(value) || !isArray(value) || !isBoolean(value) || !isNumber(value)">This is Simple Text !</p>


Yes, you can do it but now directly in template. Just create a method in controller to check for a type:

import {Component} from '@angular/core'@Component({  selector: 'my-app',  providers: [],  template: `    <div>      <div [ngSwitch]="checkType(name)">        <p *ngSwitchCase="'string'">is a string!</p>        <p *ngSwitchDefault>default</p>      </div>    </div>  `,  directives: []})export class App {  constructor() {    this.name = 'Angular2 (Release Candidate!)'  }  checkType(value) {    return typeof value  }}

Working Plunker


Please update your Angular to RC version first.