Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular UI Router state.go parameters not available in stateChangeStart

I'm using Angular UI Router and I need to send a parameter with the state.go method. Like this:

$state.go('myState', { redirect : true });

I also need to check that parameter in the event stateChangeStart. Like this:

$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
    //redirect parameter is not available here.
    //should be in toParams right?
}

Edit: here's my statedefinition:

    $stateProvider.state('customer.search', {
        url: '/search',
        views: {
            "right-flyover@": {
                templateUrl: 'customer/search/search.tpl.html',
                controller: 'CustomerSearchCtrl'
            }
        },
        data: {
            pageTitle: 'Sök användare',
            hidden: true
        }
    });
like image 576
Björn Andersson Avatar asked Aug 26 '14 10:08

Björn Andersson


2 Answers

The ui-router will pass parameters which were defined for a state hierarchy - we are navigating to. Please check:

URL Parameters (cite:)

Often, URLs have dynamic parts to them which are called parameters. There are several options for specifying parameters. A basic parameter looks like this:

$stateProvider
    .state('contacts.detail', {
        url: "/contacts/:contactId",
        templateUrl: 'contacts.detail.html',
        controller: function ($stateParams) {
            // If we got here from a url of /contacts/42
            expect($stateParams).toBe({contactId: 42});
        }
    })

So, if you want to work with param redirect, your state should look like this:

$stateProvider.state('customer.search', {
    url: '/search/:redirect',
    ...        
});

then we can use:

$state.go('customer.search', { redirect : true });

and that would be part of $statePrams

But maybe you try to use sent options, not parameters:

  • $state.go(to [, toParams] [, options]) (cite:)

options

Object - If Object is passed, object is an options hash. The following options are supported:

  • location Boolean or "replace" (default true), If true will update the url in the location bar, if false will not. If string "replace", will update url and also replace last history record.
  • inherit Boolean (default true), If true will inherit url parameters from current url.
  • relative stateObject (default $state.$current), When transitioning with relative path (e.g '^'), defines which state to be relative from.
  • notify Boolean (default true), If true will broadcast $stateChangeStart and $stateChangeSuccess events.
  • reload v0.2.5 Boolean (default false), If true will force transition even if the state or params have not changed, aka a reload of the same state. It differs from reloadOnSearch because you'd use this when you want to force a reload when everything is the same, including search params.

And that would be then the third param (reload instead of redirect):

$state.go('myState', null, { reload: true });
like image 184
Radim Köhler Avatar answered Nov 07 '22 23:11

Radim Köhler


I recently solved the problem by using cache attribute is stateProvider and set it to false like

 .state('home.stats', {
        cache: false,
        url:'/stats',
        templateUrl: 'views/stats.html',
        controller: 'StatsCtrl',
        controllerAs: 'stats'
      })
like image 1
Daniyal Avatar answered Nov 08 '22 01:11

Daniyal