9

I have a route with parameter defined:

$stateProvider
.state('item', {..})
.state('item.view', {
  url: 'item/:id'
  template: '...',
  controller: '...'
});

While already in item.view state, I would like to switch the url with

$state.go('item.view', {id: 'someId'})

without reloading the page. I have tried:

$state.go('item.view', {id: 'someId'}, {notify: false});
$state.go('item.view', {id: 'someId'}, {notify: false, reload: false});

Both still cause the page to reload. I think i may be running into the issue described here: https://github.com/angular-ui/ui-router/issues/1758

ltfishie
  • 2,917
  • 6
  • 41
  • 68
  • 3
    take a look at this SO post: [link](http://stackoverflow.com/questions/23585065/angularjs-ui-router-change-url-without-reloading-state) it appears you can use `$state.transitionTo` (instead of `$state.go`) and set optional parameters to avoid the reload. One of the comments (with the highest number of votes) in that link suggests the following: `$state.transitionTo('.detail', {id: newId}, { location: true, inherit: true, relative: $state.$current, notify: false }) so basically set notify to false and location to true – Arjen de Vries Oct 6 '14 at 15:13` credit to the author of that comment. – Shehryar Abbasi May 17 '15 at 04:52

1 Answers1

4

That should be easy enough:

// It needs to be executed on main scope, hence $timeout
// If you are calling from a controller this is a must
// Otherwise it will 'lag' and update on next function call
$timeout(function() {
    // As Sheryar Abbasi commented, the rest is simple.
    // You should call $state.transitionTo with notify: false
    // to stop the reload.
    $state.transitionTo(
        'item.view', 
        { 
            id: 4378 // New id/$stateParams go here 
        }, 
        { 
            location: true, // This makes it update URL
            inherit: true, 
            relative: $state.$current, 
            notify: false // This makes it not reload
        }
    ); 
});

This StackOverflow thread helped me figure out the $timeout.

Here is the official ui-router quick reference.

Community
  • 1
  • 1
Djuka
  • 505
  • 5
  • 19