FemiHiro
FemiHiro

Reputation: 76

Is it possible to use variable with toastController in Ionic or Angular?

I want to present a toast message to the user with his first name that I have in a variable. Is it possible? If so, how?

I want to do something like that:

this.toastCtrl.create({
  message: "Welcome user.firstname" ,
  duration: 3000
}).present();

where user.firstname contains my user's firstname.

PS: The code above is not working, it shows the message "Welcome user.firstname"

Upvotes: 2

Views: 1308

Answers (2)

Mathyn
Mathyn

Reputation: 2600

Simply do:

this.toastCtrl.create(
    { 
        message: "Welcome " + user.firstname, 
        duration: 3000 
    }
).present();

Upvotes: 1

Tomas Vancoillie
Tomas Vancoillie

Reputation: 3868

You can add parameters to the toast

async toastCtrl(msg) {
  const toast = await this.toastController.create({
    message: msg,
    duration: 300
    });
  toast.present();
}

Call the function with a variable

this.toastCtrl('Welcome ' + user.firstname);

Upvotes: 1

Related Questions