Adding inputs to alertController in ionic 4

Hey!

I’m migrating from ionic 3 to 4, and noticed that i cannot use the addInput() method that existed in ionic 3. Is there any way to do this? I need the ability to either make the inputs before or after the alert is made. I know the ‘inputs’-parameter can take a variable, but i’m not sure how to send it because of scoping.

Can you share some code of what you are trying to achieve?

Sure, the old ionic 3 code looked something like this:

showRadio() {
		let alert = this.alertCtrl.create();
		alert.setTitle('Choose one');
                        // TODO: Fix shitty loop
			for (let i = 0; i < this.list.length; i++) {
				let check;
				i == 0 ? check = true : check = false;
				alert.addInput({
					type: 'radio',
					label: this.list[i],
					value: this.list[i],
					checked: check
				});
			}

How can i achieve the same functionality in ionic 4? Problem is that i can’t put the for loop into the ‘create’ method in 4.

You can make an array of radio options and attach it to the inputs parameter while making the alert.

showRadio(){
  let radio_options = [];
  for(let i=0;i<this.list.length;++i){
     radio_options.push({
      type: 'radio',
      label : this.list[i],
      value : this.list[i],
     checked : i === 0
    });
  }

  let alert = this.alertCtrl.create({
      title : 'Choose one',
      inputs : radio_options ,
      // buttons: if you like here
  });
  
 alert.present();
}
1 Like

That seems to have worked, thanks! I was too into using ‘this’, so that’s why i thought that wouldn’t work.

1 Like