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

**URL:** <https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515>\
**Category:** ionic-v3\
**Created:** [August 4, 2016, 12:59pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515 "2016-08-04T12:59:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaejun](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jaejun/32/102071_2.png) [@jaejun](https://forum.ionicframework.com/u/jaejun)\
**Post date:** [August 4, 2016, 12:59pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515/1 "2016-08-04T12:59:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vaibsVB](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/vaibsvb/32/102515_2.png) [@vaibsVB](https://forum.ionicframework.com/u/vaibsVB)\
**Post date:** [August 4, 2016, 1:23pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515/2 "2016-08-04T13:23:19Z")

</div>

Sorry, for less understanding about your raised topic.

But Try below link. This might help you.

> [@](#):
>
> [http://mcgivery.com/understanding-ionic-2-navigation-navcontroller/](http://mcgivery.com/understanding-ionic-2-navigation-navcontroller/)

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

---

<div class="post-metadata">

**Author:** ![membersheep](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/membersheep/32/102160_2.png) [@membersheep](https://forum.ionicframework.com/u/membersheep)\
**Post date:** [August 4, 2016, 1:36pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515/3 "2016-08-04T13:36:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 4, 2016, 7:21pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515/4 "2016-08-04T19:21:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [August 4, 2016, 8:21pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515/5 "2016-08-04T20:21:32Z")

</div>

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';

        @Component({
            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!

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 4, 2016, 8:35pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515/6 "2016-08-04T20:35:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [August 4, 2016, 8:46pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515/7 "2016-08-04T20:46:33Z")

</div>

I know… force of habit. 🙂

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [August 4, 2016, 8:49pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515/8 "2016-08-04T20:49:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 4, 2016, 8:58pm UTC](https://forum.ionicframework.com/t/how-can-ionic-share-data-between-pages-or-componenets-with-ng-or-something-related-ionic2/59515/9 "2016-08-04T20:58:06Z")

</div>

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

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`.
