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

How to access $stateParams from component #36

Closed
matejkramny opened this issue May 15, 2017 · 2 comments
Closed

How to access $stateParams from component #36

matejkramny opened this issue May 15, 2017 · 2 comments

Comments

@matejkramny
Copy link

Thanks for your efforts on making this available.

How can the (ng2+) component access the $stateParams service and $rootScope potentially?

I tried importing StateParams from @uirouter/angular but it seems like that module is not available because angular complains.

@christopherthielen
Copy link
Member

If it's a routed component, it's best to inject the Transition and get transition.params().

constructor(trans: Transition) {
  const params = trans.params();
}

Otherwise you can inject UIRouterGlobals and access UIRouterGlobals.params. Globals aren't updated until after a transition has completed successfully.

constructor(globals: UIRouterGlobals) {
  const params = globals.params;
}

To get the $rootScope you will need to make the AngularJS service available to Angular, as described here: https://angular.io/docs/ts/latest/guide/upgrade.html#!#making-angularjs-dependencies-injectable-to-angular

The easiest way is to inject the AngularJS injector and get it from there:

constructor(@Inject('$injector') $injector) {
  const $rootScope = $injector.get('$rootScope');
}

@matejkramny
Copy link
Author

Works, many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants