Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ui-router does not see url search params after page refresh

I have a state that looks like something like this

.state('home.foo.bar', {
  url: 'view?one&two',
  views: {
    'base': {
      templateUrl: 'blah',
      controller: 'blahCtrl'
    }
  },
  reloadOnSearch: false
})

if I navigate to this state in my template with

ui-sref="home.foo.bar({one: valueOne, two: valueTwo})"

all is well and the url is

.com/view?one=valueOne&two=valueTwo

however if I refresh the page the url changes to just .com/view and neither $state.params or $stateParams contains the values.

if I add a resolve step like so

.state('home.foo.bar', {
  url: 'view?one&two',
  views: {
    'base': {
      templateUrl: 'blah',
      controller: 'blahCtrl'
    }
  },
  resolve: {
    p: function($location, $stateParams, $state) {
      console.log($location.search(), $stateParams, $state);
      if(!$stateParams.one && !$stateParams.two {
        $stateParams.one = $location.search().one;
        $stateParams.two = $location.search().two;
      } else {
        return $location.search().one;
      }
    }
  },
  reloadOnSearch: false
})

then when I initially navigation to the state in my template the log statement prints nothing for $location.search() but shows oneValue and twoValue in the $stateParams and $state.params. When I refresh its opposite $location.search() shows the parameters and their values correctly but $stateParams and $state.params are empty.

adding the if statement and setting the values explicitly resolves my bug but this cant be working as intended can it?

am I missing something obvious?

like image 514
Mark Avatar asked Nov 09 '22 14:11

Mark


1 Answers

I think it might be because you are missing a slash at the beginning or your URL:

.state('home.foo.bar', {
  url: '/view?one&two',
  views: {
    'base': {
      templateUrl: 'blah',
      controller: 'blahCtrl'
    }
  },
  reloadOnSearch: false
})
like image 64
Thomas Roch Avatar answered Nov 14 '22 22:11

Thomas Roch