Pushing data to a single object instance


First time building an Ionic 2 app and I was looking for some advice.

For simplicity’s sake, I have created a project in which I will implement on a bigger scale in my real project.

I have two pages, one collects a name and another collects the age. I am attempting to use a provider that holds an object like personData into which I can push the user submitted values to.

How do I achieve this? Basically, I want to push the data from each page to a single object instance that has those attributes already defined. ( I would then push the object to an object array and save it using Ionic’s storage module)

I have a provider called database which holds dummy data:

export class Database {

  database: any;

  constructor(public http: Http) {
    console.log('Hello Database Provider');
    this.database = [
      {name: 'Homer', age: '45'},
      {name: 'Candace', age: '23'},
  loadAll() {
    return Promise.resolve(this.database);

how do I push to this provider? (and is this even the best way to do this?)
Right now I am pushing the data from page to page using navController, but it seems like there should be a better way.


Inject the provider into both pages. (Search Angular Dependency Injection.) Make sure the provider is declared in app.module.ts. If you do that, the same provider will be injected into both pages. If you don’t do that, two different copies of the provider will be injected, one into each page. And you want one bucket to pour data into. In your provider, have a public method like pushData(data: TypeOfDataToPush) {} and both pages call that method with their data. They can retrieve it similarly.

Edited to add: This is much better than using navParams if you are programming for a desktop browser. The data in a provider will survive a browser page refresh, while the data in navParams will not.