How can ionic share data between pages or componenets with ng or something related ionic2?

I’ really curious how to share data which i selected between components.

When I use ng1, it is easy to share it with scope or rootScope. But now I’d rather to use ionic2.

I confront to this problem.

For example,

I made menu. it can go anywhere with item id and item title with push or setRoot method,

but i can not share item id with menu component.

They were divided each other according to this ionic system.

How can I solve this problem?

Sorry, for less understanding about your raised topic.

But Try below link. This might help you.

This article helps you for sharing your data/array while navigation between pages/components.

I would use a service (injected as provider) to handle data. While you can pass data with the push method thanks to the navigation controller you can’t pass it with the pop method.

P.S.: If you inject the service as provider in the parent component (like the app component) it will be automatically accessible to all its child components and it will be the same instance of the service.

I don’t think a service is appropriate for something like “current item id”. I would pass that around using navigation parameters.

For simple cases, you should do as @rapropos says, pass via NavParams.

When you need to share more data, you can use a service provider.

In the terminal, type:

ionic generate provider MySharedObject

Then in the app.ts:

import {MySharedObject} from './providers/my-shared-object/my-shared-object';

            templateUrl: 'build/app.html',
            providers: [MySharedObject]

Finally, when you need to give access to a given *.ts page:

import {MySharedObject} from '../../providers/my-shared-object/my-shared-object';

        mySharedObj: any;

        constructor(private data: MySharedObject) {
            this.mySharedObj = data;

Good luck!

When you put an access modifier on a constructor parameter, as you did with private data: MySharedObject, TypeScript automatically creates a private member called data and initializes it for you. You don’t need to (and shouldn’t) declare or assign to mySharedObj explicitly.

I know… force of habit. :slight_smile:

It’s a part of the language I dislike. It’s akin to having implicit getters and setters in other languages. I like to see the member variables explicitly written in all classes.

[quote=“ryanlogsdon, post:8, topic:59515”]I like to see the member variables explicitly written in all classes.

That’s cool, but if you’re going to do it that way, leave the private off the constructor parameter and give the property a proper type instead of any.