user1998013 user1998013 - 1 year ago 176
TypeScript Question

how to add a dynamic button using navparams in ionic 2

I am building an app in Ionic 2, I am using navparams tu grab the button pressed when pushing another page.
In this page I have a link that I want to generate dynamically using the params passed depending the buton pressed in the previous page.

So, I have homepage with button1, button2, button3.
In homepage I have this code:

firstPassed: "param1"

Then in the next Page I have:

export class Page {
public firstParam:any;
constructor(public navCtrl: NavController, params: NavParams) {
this.firstParam = params.get("parampassed");

If I add in the page.html:


I get the params well, but what I want is to use that param as a link in a button, something like this in the page.ts:


So it could take me to the page pageparam1.html

How can I do it?

Thank you

Answer Source

create seperate pages (including .ts files) for Page 1, Page 2 and Page 3

Next import these in your 'NextPage'

make a button <button (click)="goToPageWithParam()">Go to param page</button>

Now in your NextPage create this function:

    let page:any;
    if(this.firstParam() == "button1"){
        page = Page1;
    if(this.firstParam() == "button2"){
        page = Page2;

    //then at the end

I think this is a solution to your problem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download