I want to switch between segments on button click. Any idea how I can achieve this on ionic 2?
Thanks
I want to switch between segments on button click. Any idea how I can achieve this on ionic 2?
Thanks
<ion-segment [(ngModel)]="sections.selectedSection">
<ion-segment-button value="{{sections.first}}">
First section
</ion-segment-button>
<ion-segment-button value="{{sections.second}}">
Second section
</ion-segment-button>
</ion-segment>
Then in your class define the sections
object:
public sections: any = {
first: 'first',
second: 'second',
selectedSection: ''
};
Then just change the selected section in your button click e.g.:
buttonClickHandler() {
this.selectedSection = this.sections.second;
}
This is how I do it.
Thank you so much! It is working but is there a way we can change âisActiveâ attributeâs value in the same click function? @Daveshirman
Sorry, donât really understand your question.
tabs icons are not looking like they are inactive.
It is now hard for me to show the active segment to the user. Could you please tell me how you managed to resolve this? Thanks @Daveshirman !
Sorry, my mistake:
should have been:
buttonClickHandler() {
this.sections.selectedSection = this.sections.second;
}
This will fix the active styling issue you mentioned.
Hi if I follow your method, however, by button was on a previous page. How do I go about? I am using lazy loading
Not sure what you mean. But sounds like you want to use Events. Look them up in the Ionic API docs.
Something like on page 1 having 2 buttons. So when I click on â2â button it will link me to another page with segment 2 selected.
Thankyou in advance.
Ah yes, so you should pass in a parameter with the segment value you want
to your next page that loads up, and just use that parameter to set the
current segment value.
Thatâs exactly what Iâve done as I did the same as you in some cases.
Could you teach me how to do it sorry, am really new to programming.
Take a look at:
âPushing a viewâ here:
It shows you how to push a new âPageâ onto the stack with parameters.
Then just assign that parameter to the segment value from my example above
in this thread.
I havenât got time to teach you programming! Read the docs / download the
Ionic starter tutorial app and see how it works by playing with it, youâll
get all you need from that
Hi, This is what I come up with what am I missing? Sorry to disturb you.
Home.html
<ion-header>
<ion-navbar>
<ion-title>home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button (click)="navigateTo1()">1</button>
<button ion-button (click)="navigateTo2()">2</button>
</ion-content>
Home.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html',
})
export class HomePage {
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad HomePage');
}
navigateTo1(){
this.navCtrl.push('SegmentPage', {param1: '1'});
}
navigateTo2(){
this.navCtrl.push('SegmentPage', {param1: '2'});
}
}
Segement.html
<ion-header>
<ion-navbar>
<ion-title>segment</ion-title>
</ion-navbar>
<ion-toolbar>
<ion-segment [(ngModel)]="status">
<ion-segment-button value="1">1</ion-segment-button>
<ion-segment-button value="2">2</ion-segment-button>
</ion-segment>
</ion-toolbar>
</ion-header>
<ion-content [ngSwitch]="status">
<ion-grid *ngSwitchCase="'1'">
<ion-row>
<ion-col>
<div>
<p>This is page 1</p>
</div>
</ion-col>
</ion-row>
</ion-grid>
<ion-grid *ngSwitchCase="'2'">
<ion-row>
<ion-col>
<div>
<p>This is page 2</p>
</div>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
Segment.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
@IonicPage()
@Component({
selector: 'page-segment',
templateUrl: 'segment.html',
})
export class SegmentPage {
parameter1: any;
status: any = this.parameter1;
constructor(public navCtrl: NavController, public navParams: NavParams) {
this.parameter1 = navParams.get('param1');
}
//status: any = this.parameter1;
ionViewDidLoad() {
console.log('ionViewDidLoad SegmentPage' + this.parameter1);
}
}
Firstly, your push lines are wrong: you donât need quotes around the page
name, and your object doesnât appear formatted correctly either. Also I
canât see where youâve imported the page component at the top at all.
Secondly, in your next page, I donât know what youâre doing two variables.
Just store the incoming param value into status and in your ngSwitch lines,
just put an inline condition e.g status == 2
Ignore comment about the param object, itâs fine, misread.
i have a very simple question.
<ion-segment [(ngModel)]=âpetâ (ionChange)=âonSegmentChange($event)â>
<ion-segment-button value="takeway">
Take Away
</ion-segment-button>
<ion-segment-button value="dinein">
Dine in
</ion-segment-button>
when i selected a specific value in the segment i should pass the value to another page.
any help will be appreciated.
Thankyou
Doesnât seem that way to me at all.
Why do you need to do this? What other places might be interested in this information? When and how is âanother pageâ activated? How persistent is this selection? What other places might change it?
Your question is actually extremely broad and has no single âone size fits allâ answer.
I need to get all this information in the modal page which is the final page.
no other places are effected from this
.
I canât make much sense out of this, but have you looked at how to use componentProps
to pass data to modals?