How to call custom function after ngSwitch new view is created

I am creating a small app using Angular2+Ionic2. In this app I want initialise google-map into view segment when user switch to the map-segment using ion-segment

Following is the sample code structure:

<ion-navbar *navbar class="hide-navbar">
    <ion-segment [(ngModel)]="homeSegment">
        <ion-segment-button value="list">
        <ion-segment-button value="map" >
    <div [ngSwitch]="homeSegment" >
        <div *ngSwitchWhen="'map'">
            <div id="googleMap"></div>
        <ion-list *ngSwitchWhen="'list'">

I have tried by providing click listener for ion-segment-button, but that doesn’t worked out. Before div with id=“googleMap” is appended to DOM, functionality for adding map gets triggered, and which result a undefined error.

So, how we can understand when new elements are loaded when an ngSwitch happens? Whats the best way to do it?

@piccaza I was trying something similar. Did you manage to figure out a way?

Never mind, I used the change output event as in the docs:

Simply add (change)="onSegmentChanged($event) to the ion-segment, then create a onSegementChanged method in your class. This will be called. The value of the segment will be passed in the event, so just check for ‘maps’ or whatever you called in your segment, and run your code accordingly.


You can use (select) on ion-segement-button to run custom function for the selected segment.


1 Like

Yes :relaxed: , see this SO item:

Finally how I resolved this is by, by providing a (change)=“onSegmentChanged($event)” on ion-segment element.

Which will trigger whenever ion-segment will switch. And I am able to access the dom element with id googleMapAllFobs when its getting switched to value=“map”.

Provided a checking in onSegmentChanged function, before call load google map function, to ensure the current view contains div to load google map.

Looks very easy for this current requirement.

1 Like

Hi there, could you share your code with us? I’m still looking for a solution… my code is still not working, what I’m doing wrong?
This is my code:

   <ion-segment [(ngModel)]="dashboard" (change)="onSegmentChanged($event)">
        <ion-segment-button value="list">
        <ion-segment-button value="map">

  <div [ngSwitch]="dashboard">

    <div *ngSwitchCase="'list'">
        some content...
      <div *ngSwitchCase="'map'">
        <div #map id="map"></div>



 initMap() {

        let LatLng = new google.maps.LatLng(52.491114, 13.394625);

        let mapOptions = {
            center: LatLng,
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }; = new google.maps.Map(this.mapElement.nativeElement, mapOptions);

    onSegmentChanged($event) {
        if ($event == 'map') {



Would really appreciate, thank you!

I consider this template abuse. Templates are view layer animals that depict a state of affairs and accept input. ngSwitch is not a control flow concept, so trying to trigger component code actions from it does not make sense.

I’m not sure what you’re trying to say. But handling segments with ngSwitch is the common way (according to the ionic docs)

1 Like