paint-brush
Passing Data Between Pages in an Ionic Applicationby@amanhimself
49,338 reads
49,338 reads

Passing Data Between Pages in an Ionic Application

by Aman MittalAugust 11th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In the previous posts, we have seen<a href="https://hackernoon.com/https-medium-com-amanhimself-basic-navigation-in-ionic-applications-ecb199cdf15b" target="_blank"> how to setup a basic navigation between multiple Ionic app Pages</a>. This post concerns what if you want to send some data from the previous page to the next page in the stack? For the Ionic provides <code class="markup--code markup--p-code">NavParams</code> class to transfer data from one page to another.

Company Mentioned

Mention Thumbnail
featured image - Passing Data Between Pages in an Ionic Application
Aman Mittal HackerNoon profile picture

In the previous posts, we have seen how to setup a basic navigation between multiple Ionic app Pages. This post concerns what if you want to send some data from the previous page to the next page in the stack? For the Ionic provides NavParams class to transfer data from one page to another.

Generate the application

In this demo application we will first setup a home page with a text box to enter data that will be transfered to the next page. First, let’s generate a new Ionic application:

Create a new about page:

And lastly, to complete our setup, we must add about page in the app module:

Add Input Text in Home Page

Then we will update home.html:

#color is a local variable whose value we will be referencing to pass on to the next page in our navigation stack. We will now update our home.ts with business logic behind the only click event in our template:

Note the second argument in this.navCtrl.push() which is being used to pass the data.

About Page

To Pass data from Home page to About page we will need to import NavParams class. Since, I am using Ionic CLI to generate pages, class NavParams will already be imported in the about page.

Display Fetched Data

To catch the data from the previous page in the navigation stack, we are using get() method of NavParams class. We fetch data inside the constructor function of AboutPage class.

Finally, to display data on about page:

Summary

Here are some screenshots:

Home Page:

User Input being entered:

Data passed form Home Page displayed on About Page:

When nothing entered in the input field, a default text passed and displayed:

To get the full code of this demo app, you can visit this Github Repository.

Want to read more articles like this one? Subscribe here. Sometimes, I send “never seen before” content to my subscribers.

If you like this article, please clap 👏 .

Thank you!