ionic3ionic4ion-segment

Ion-Segment in Ionic4 vs Ionic3


I have a segment of the form given below in Ionic3 and the code given below. Could any one please tell me how to implement the same in Ionic4.

Ionic3 code

    <ion-header>
  <ion-navbar>
    <ion-buttons left>
      <button ion-button icon-only menuToggle="user-menu">
        <ion-icon name="menu"></ion-icon>
      </button>
    </ion-buttons>
    <ion-buttons end>
      <button ion-button icon-only (click)="doLogout()">
        <ion-icon name="ios-log-out" color="primary"></ion-icon>
      </button>
    </ion-buttons>
  </ion-navbar>
  <div text-center (click)="editProfile()">
    <img class="user-avatar" src="assets/imgs/avatar.svg" />
  </div>
  <ion-toolbar>
    <ion-segment [(ngModel)]="category" color="primary">
      <ion-segment-button value="official">
        Official Info
      </ion-segment-button>
      <ion-segment-button value="personal">
        Personal Info
      </ion-segment-button>
    </ion-segment>
  </ion-toolbar>
</ion-header>

<ion-content padding>
  <div [ngSwitch]="category">
    <ion-list *ngSwitchCase="'personal'">
      <ion-item>
        <h4>First Name</h4>
        <p>X</p>
      </ion-item>
      <ion-item>
        <h4>Last Name</h4>
        <p>YZ</p>
      </ion-item>
      <ion-item>
        <h4>Phone No</h4>
        <p>(541) 754-3010</p>
      </ion-item>
      <ion-item>
        <h4>Address</h4>
        <p>XYZ 711-2880 Nulla St. Mankato Mississippi 96522</p>
      </ion-item>
    </ion-list>

    <ion-list *ngSwitchCase="'official'">
      <ion-item>
        <h4>User Id</h4>
        <p>Xyz</p>
      </ion-item>
      <ion-item>
        <h4>Email</h4>
        <p>Xyz@abc.com</p>
      </ion-item>
      <ion-item>
        <h4>Employer</h4>
        <p>Amazon</p>
      </ion-item>
      <ion-item>
        <h4>Company Name</h4>
        <p>Amazon India</p>
      </ion-item>
      <ion-item>
        <h4>Company Id</h4>
        <p>{{nowDate|date:'hh:mm:a'}}</p>
      </ion-item>
    </ion-list>
  </div>
</ion-content>

enter image description here

Ionic 4 code

    <ion-toolbar>
  <ion-segment (ionChange)="segmentChanged($event)">
    <ion-segment-button value="camera">
      <ion-icon name="camera"></ion-icon>
    </ion-segment-button>
    <ion-segment-button value="bookmark">
      <ion-icon name="bookmark"></ion-icon>
    </ion-segment-button>
  </ion-segment>
</ion-toolbar>

In the Ionic4 officeial documentation they mention only about creating the segment buttons and didnt specify how to populate list based on that. Is it similar to Ionic3 using *ngSwitchCase


Solution

  • You can continue using the *ngSwitch as before, what you have to do is in the event (ionChange)="segmentChanged($event)" change the value you want to show as you used to do with [(ngModel)]="category".

    Add this method in component:

    segmentChanged(event) {
      this.category = event;
    }
    

    "event" is the value of the case that you have put in the ion-segment-button value="camera".

    More information: https://angular.io/api/common/NgSwitchCase