NgSwitch

1)app.component.html

<p> {{title}} </p>
<div class='panel panel-primary'>
    <div class='panel-heading'>
        <p>Simple example of ngSwitch </p> 
    </div>
   <div class="panel-body">
       Input string : <input type='text' [(ngModel)] ="num"/>

        <div [ngSwitch]="num">
            <div *ngSwitchCase="'1'">One</div>
            <div *ngSwitchCase="'2'">Two</div>
            <div *ngSwitchCase="'3'">Three</div>
            <div *ngSwitchCase="'4'">Four</div>
            <div *ngSwitchCase="'5'">Five</div>
            <div *ngSwitchDefault>This is Default</div>
        </div>
    </div>
</div>
<div class='panel panel-primary'>
    <div class='panel-heading'>
        <p>Simple example of ngSwitch </p> 
    </div>
   <div class="panel-body">
        <div class='row'>
            <div class='col-md-6'>
                <select [(ngModel)]="selectedValue">
                    <option *ngFor="let item of items;" [value]="item.name">{{item.name}}</option>
                </select>
            </div>
        </div>

        <div class='col-md-6'>            
            <div class='row' [ngSwitch]="selectedValue">
                <div *ngSwitchCase="'One'">One is Pressed</div>
                <div *ngSwitchCase="'Two'">Two is Selected</div>
                <div *ngSwitchDefault>This is Default</div>
            </div>
        </div>
    </div>
</div>

2)app.component.ts

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
  selector: 'app',
  templateUrl: './app.component.html',
})
export class AppComponent
{
    title: string = 'ngSwitch Example' ;
    num: number= 0;
    items: item[] = [{name: 'One', val: 1}, {name: 'Two', val: 2}, {name: 'Three', val: 3}];
    selectedValue: string= 'One';
}
class item {
  name: string;
  val: number;
}

3)app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

results matching ""

    No results matching ""