Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rootParams in ion-nav not returning data #9714

Closed
annalynayop opened this issue Dec 19, 2016 · 17 comments
Closed

rootParams in ion-nav not returning data #9714

annalynayop opened this issue Dec 19, 2016 · 17 comments
Assignees
Labels
ionitron: v3 moves the issue to the ionic-v3 repository

Comments

@annalynayop
Copy link

annalynayop commented Dec 19, 2016

Ionic version: (check one with "x")
[ ] 1.x
[ x] 2.x

I'm submitting a ... (check one with "x")
[x ] bug report
[ ] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or http://ionicworldwide.herokuapp.com/

Current behavior:

Passing data in rootParams
Expected behavior:

Return data

Related code:
Page 1:
this.test = {x: 'For Testing'};

<ion-nav [root]="rootPage" [rootParams]="test"></ion-nav>

Page 2:
console.log(this.navParams); // no return data
or
console.log(this.navParams.get('x')); // no return data

Ionic info:
System information:
Cordova CLI: 6.4.0
Ionic Framework Version: 2.0.0-rc.4
Ionic CLI Version: 2.1.17
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.47
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 7
Node Version: v6.9.1
Xcode version: Not installed

@jgw96 jgw96 added the v2 label Dec 21, 2016
@warent
Copy link

warent commented Dec 23, 2016

this is happening to me as well...

@annalynayop
Copy link
Author

Hi @warent since it's still not working, I tried in this doc http://ionicframework.com/docs/v2/api/navigation/NavController/ the Navigating from the Root component . Maybe this can help you too.

@mordka
Copy link

mordka commented Apr 20, 2017

This is still an issue in Ionic 3.0.1
I found that the rootParams are available in NavController instance, but rootParam property is missing in the typing. This works:
(navCtrl as any).rootParams

@manucorporat manucorporat self-assigned this Apr 20, 2017
@manucorporat
Copy link
Contributor

manucorporat commented Apr 20, 2017

can someone provide a small plunker or repo that reproduces the issue?

@warent
Copy link

warent commented Apr 20, 2017

@manucorporat Reproducing this is really trivial. @zuna003 provided reproduction steps

@alexbainbridge
Copy link

FYI this is working as expected, however the timing is off.

Going back to the original example, if you wait (e.g. 1.5 seconds) from setting, to navigating to the second page, the navparams value is there. If you navigate immediately, it is not.

It doesn't appear to be a zone thing.

[Timings from actual device]

@aggarwalankush
Copy link
Contributor

aggarwalankush commented May 29, 2017

@manucorporat I found the solution for this issue. In line
https://github.com/driftyco/ionic/blob/59eb9a328d353326540d767921057a887058d66f/src/components/nav/nav.ts#L145, root is set without rootParams.

I changed it from this.setRoot(page); to this.setRoot(page, this.rootParams); and resolved my issue. Can you please look into this and add a proper fix?

+Adding a linked issue #6530

@danielsogl
Copy link
Contributor

@aggarwalankush I created a PR with your solution.

@aggarwalankush
Copy link
Contributor

aggarwalankush commented May 29, 2017

@danielsogl This is not a good solution. I tried to point out the problematic code. As both root and rootParams are @Input properties, rootParams might not be initialized while setting root so this.setRoot(page, this.rootParams); might not always work.

A good solution would be taking below logic out of setter and put somewhere else. Might be in ngOnChanges()

    if (this._hasInit) {
      this.setRoot(page);
    }

@aggarwalankush
Copy link
Contributor

aggarwalankush commented May 29, 2017

@danielsogl @manucorporat instead of setting rootPage, I'm using following code and it works.

this.appCtrl.getRootNav().setRoot(root, { myParams: someParamsObject });

Let me know if it's the good solution.

@sharonswli
Copy link

sharonswli commented Aug 4, 2017

So I got it working with the following code (see below). I'm facing the same issue as @alexbainbridge described: sometimes calling this.navParams.data on my Tabbed page returns the object I have set in Tab.ts as expected, and sometimes it just returns an empty object, causing template errors in my tabbed page.

Anyone has any idea what's causing this? Is there another solution/workaround that ensures data is passed into the tabbed page every time? The inconsistency is frustrating and unacceptable when running the app in production.

Login.ts

login() {
  this.app.getRootNav().setRoot(TabsPage, { user: userData });
}

Tabs.ts

export class TabsPage {
  dashboardRoot: any = 'Dashboard';
  dashboardParams: any = {
    user: this.navParams.get('user')
  }
}

Tabs.html

<ion-tabs [selectedIndex]="activeTabIndex">
  <ion-tab [root]="dashboardRoot" [rootParams]="dashboardParams"></ion-tab>
</ion-tabs>

Dashboard.ts

ngOnInit() {
  this.user = this.navParams.get('user'); // Unreliable but returns the user object
}

@baladin
Copy link

baladin commented Sep 22, 2017

i am facing the same issue. My app.html is
<ion-nav [root]="rootPage" [rootParams]="data">
rootPage navParams is empty.

@jo-soft
Copy link

jo-soft commented Dec 12, 2017

@amirhammad: i have a similar issue: it's working fine the first time. but if you change the value rootPage it fails.

          <ion-nav [root]="selectedTab.component" [rootParams]="selectedTab.params"></ion-nav>

step 1: set selectedTab to some value T1 with param = P1 - everything works fine
step 2: change the value of selectedTab, say to T2
step 3: change the value of selectedTab back to T1. - NavParams in the component is empty ({}) because it calls change on root ignoring the params.

@stillatmylinux
Copy link

What helped me was to move my this.navParams out of the constructor function and into the ngOnInit() function.

 constructor(
        private navParams
 ) { 
      console.log(typeof this.navParams) // undefined
 }

 ngOnInit() {
     console.log(typeof this.navParams) // object
 }

@rodpatulski
Copy link

Still had this problem and the workaround for me was to not rely on the 'rootPrams' property in the it just didn't work for me.

What DID work for me was:
Page: 1 (my app.component.ts)
ngAfterViewInit(): void { this.nav.setRoot(WelcomePage, {'fake': 'fakeValue'}); }

WelcomePage:
-- in the constructor...
navParams.get('fake');

The trick was to explicitly call setRoom in app.component.ts versus relying on the name matching from .

@adamdbradley adamdbradley added the ionitron: v3 moves the issue to the ionic-v3 repository label Nov 1, 2018
@imhoffd imhoffd removed the ionitron: v3 moves the issue to the ionic-v3 repository label Nov 28, 2018
@Ionitron Ionitron added the ionitron: v3 moves the issue to the ionic-v3 repository label Nov 28, 2018
@ionitron-bot
Copy link

ionitron-bot bot commented Nov 28, 2018

This issue has been automatically identified as an Ionic 3 issue. We recently moved Ionic 3 to its own repository. I am moving this issue to the repository for Ionic 3. Please track this issue over there.

If I've made a mistake, and if this issue is still relevant to Ionic 4, please let the Ionic Framework team know!

Thank you for using Ionic!

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 28, 2018

Issue moved to: ionic-team/ionic-v3#152

@ionitron-bot ionitron-bot bot closed this as completed Nov 28, 2018
@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ionitron: v3 moves the issue to the ionic-v3 repository
Projects
None yet
Development

No branches or pull requests