Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

The entry point to my application is an URL with an unknown parameter value, e.g.:

http://www.host.com/key/25

and my simplified configuration looks like this:

$stateProvider.state('home', {
        url: "/key/:key",
        component: 'home'
}).state('sitenotice', {
        url: "/sitenotice/",
        component: 'sitenotice'
});

I try to navigate to "sitenotice" and back to "main" in the index.html with:

<html ng-app="myApp">
  ...
  <div ui-view></div>

  <a ui-sref="sitenotice">site notice</a>
  <a ui-sref="home">home</a>
</html>

It is not working, because I don't pass the key-Parameter to the state "home". I have no idea how to access the initial value. Or can it be cached somehow? Is it possible to do that just with ui-router, I don't use a root-controller yet.

I use angular 1.5.8 and angular-ui-router 1.0.0-beta.3.

share|improve this question
1  
After you have get the parameter(:key) from stateParams, you can keep it in rootscope or pass it to the next state. – Pengyy Mar 1 at 8:48
up vote 0 down vote accepted

My solution is using the rootScope and store the parameter value there. I also made an update to angular-ui-router 1.0.0-rc.1. When leaving the "home"-state, $rootScope.navKey gets the current state parameter value.

angular
.module('myApp')
.run(function ($rootScope, $stateParams, $transitions) {
  $rootScope.navKey = "";

  $transitions.onExit({ exiting: 'home' }, function() {
    if (angular.isDefined($stateParams.key)) {
        $rootScope.navKey = $stateParams.key;
    }
  });
});

In my index.html I use the navKey to go back to the "main"-state:

<a ui-sref="home({ key: navKey })">
  <md-button md-no-ink="" class="md-primary">Home</md-button>
</a>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.