If you want to pass non-URL state, then you must not use url
when setting up your state
. I found the answer on a PR and did some monkeying around to better understand.
$stateProvider.state('toState', {
templateUrl:'wokka.html',
controller:'stateController',
params: {
'referer': 'some default',
'param2': 'some default',
'etc': 'some default'
}
});
Then you can navigate to it like so:
$state.go('toState', { 'referer':'jimbob', 'param2':37, 'etc':'bluebell' });
Or:
var result = { referer:'jimbob', param2:37, etc:'bluebell' };
$state.go('toState', result);
And in HTML thusly:
<a ui-sref="toState(thingy)" class="list-group-item" ng-repeat="thingy in thingies">{{ thingy.referer }}</a>
This use case is completely uncovered in the documentation, but I think it's a powerful means on transitioning state without using URLs.
I adjusted your approach below. We will profit from decorating, but not of the directive. As we can check here, in the ui-sref
directive source code, the click event in fact does call:
$state.go(ref.state, params, options);
So, we can decorate the $state
provider, which could be very straightforward:
.config(function ($provide) {
$provide.decorator('$state', function ($delegate) {
// let's locally use 'state' name
var state = $delegate;
// let's extend this object with new function
// 'baseGo', which in fact, will keep the reference
// to the original 'go' function
state.baseGo = state.go;
// here comes our new 'go' decoration
var go = function (to, params, options) {
options = options || {};
// only in case of missing 'reload'
// append our explicit 'true'
if (angular.isUndefined(options.reload)) {
options.reload = true;
}
// return processing to the 'baseGo' - original
return this.baseGo(to, params, options);
};
// assign new 'go', right now decorating the old 'go'
state.go = go;
return $delegate;
});
})
And that's enough. Now any state change (including click on ui-sref
) will trigger reload
.
Note: Just must say, that I do not think that this is the way. Triggering reload all the time ... to me it seems that we in fact loose a lot, loose the advantages coming with that diamond - ui-router
Best Answer
2 things:
$state
into yourapp.run()
Use
$stateChangeSuccess
instead.run(['$rootScope','$state', function($rootScope, $state) { $rootScope.$on('$stateChangeSuccess', function(event, next) { $state.go('root.other'); })}]);
Working plunk.