how to get entire object using navParams - Ionic 3 Angular 4
Solution 1
You don't need to do like that. You can send all at once like below.
Note: Declare a data transfer object like DtoMy
Dto-my.ts
export class DtoMy {
firstPassed: string;
secondPassed: string;
//your other properties
}
send
let dtoMy = new DtoMy();
dtoMy.firstPassed= 'firstPassed';
dtoMy.secondPassed= 'secondPassed';
//your other values
const myModal = this.modalCtrl.create('MyModalPage', { data: dtoMy });
myModal.onDidDismiss(data => { });
myModal.present();
Receive:
my-modal-page.ts
data :DtoMy;
constructor(private navParams: NavParams, private modalCtrl: ModalController) {
this.data = this.navParams.get('data');
}
Solution 2
To be able to get an entire object in the second page, you can try the following:
To send:
constructor(public navCtrl: NavController, public navParams : NavParams){}
public userObject =
{
firstName : "peter",
lastName : "haddad"
}
clickMe()
{
this.navCtrl.push(SecondPage,this.userObject);
}
You import both the NavController and the NavParams, then use the method push()
that will contain the object you want to pass and it will navigate to the second page.
In the second Page:
export class SecondPage
{
userObject;
userName;
constructor(public navCtrl: NavController, public navParams: NavParams){}
ngOnInit()
{
this.userObject = this.navParams.data;
this.userName = this.userObject.firstName;
console.log(this.userObject);
}
}
Here you can access the object by using the instance member data
of NavParams
, to be able to access the lastName
, you can do this this.lastName=this.userObject.lastName
Murlidhar Fichadia
I have recently completed my Bachelor’s degree in Computer Science at Heriot Watt University. I am a Full Stack developer and write mainly in Php, Java, C/C++ and Python. I have been freelancing for past 4 years along with university studies. Currently working at Back9 Solutions as a Full Stack Developer, I am using Laravel php framework as back-end and AngularJS, Bootstrap and UI kit or Ionic as front-end development tools.
Updated on June 09, 2022Comments
-
Murlidhar Fichadia almost 2 years
currently I am passing data object from one page to another page (in Modal).
I have 8 parameters to pass to the modal view.
this.firstParam = navParams.get("firstPassed"); this.secondParam = navParams.get("secondPassed"); . . . this.eightParam = navParams.get("eightPassed");
How can I get entire object data using one call
this.data = navParams.getAll(); //something like this
I am unable to find a method in a documentation to get an entire object.