Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$stateChangeStart not being fired on v1.0 #2720

Closed
brunolemos opened this issue Apr 28, 2016 · 18 comments
Closed

$stateChangeStart not being fired on v1.0 #2720

brunolemos opened this issue Apr 28, 2016 · 18 comments

Comments

@brunolemos
Copy link

brunolemos commented Apr 28, 2016

Works fine on v0.2.13

$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        console.log('from', fromState.name, 'to', toState.name);
});
@dreamstu
Copy link

+1 on v1.0.0-alpha0

@ergo
Copy link

ergo commented Apr 29, 2016

Same here on 1.0.0-alpha.4

@ergo
Copy link

ergo commented Apr 29, 2016

Actually...
The $stateChange* events are deprecated. The $transitions.on* hooks are generally much more powerful. The events are not included by default, but can be re-enabled by including the stateEvents.js file (missing in 1.0.0-alpha.1; see #2655) from #2219

@samithaf
Copy link

samithaf commented May 1, 2016

Don't forget to add 'ui.router.state.events' as a dependancy for your project too

@christopherthielen
Copy link
Contributor

Thanks all.

$stateChange* events are deprecated

The $stateChange* events are deprecated in 1.0. They are replaced with the $transitions.on* transition hooks.

However, they can be re-enabled for backwards compatibility by including stateEvents.js and depending on the 'ui.router.state.events' angular module.

<script src="stateEvents.js">
var myApp = angular.module('myApp', ['ui.router', 'ui.router.state.events']);

Note: the code in stateEvents.js re-implements the $stateChange* events using the new Transition Hooks

@yangmillstheory
Copy link

Are there any examples of how to use $transitions?

@christopherthielen
Copy link
Contributor

@yangmillstheory @alexleung there are some samples in the alpha docs: http://angular-ui.github.io/ui-router/1.0.0-alpha.3/interfaces/transition.ihookregistry.html

See also the sample app: https://github.com/ui-router/sample-app/tree/master/app/routerhooks

@yangmillstheory
Copy link

Thanks @christopherthielen!

@anchengjian
Copy link

Can you update the document for $stateChange* events?

@cawa-93
Copy link

cawa-93 commented Sep 15, 2016

Can you help me?
I do not understand how I can save the state. In the previous version I set the event handler like this:

$scope.$on('$stateChangeSuccess', ($event, toState, toParams) => mySaveFunction(toState, toParams));

And at startup the application loads state like this:

{toState, toParams} = myLoadFunction();
$state.go(toState, toParams);

How do I have the same effect is achieved in the new version? How can I create an event handler that will receive information about the new state?

I tried to do something like this:

$transitions.onSuccess({to: true }, ($transition, $state) => {
// ...
});

But $state always are NULL.

@christopherthielen
Copy link
Contributor

Can you update the document for $stateChange* events?

I see 10 thumbs up wanting updated docs, just letting you know they are found here: https://ui-router.github.io/docs/latest/modules/ng1_state_events.html

@christopherthielen
Copy link
Contributor

@cawa-93

$transitions.onSuccess({ }, trans => {
  mySaveFunction(trans.to(), trans.params());
});

@mackelito
Copy link

@christopherthielen perhaps it would be nice to have a way too install the stateEvents.js thru a package.json file? perhaps add it as a separate module?

@samithaf
Copy link

samithaf commented Dec 8, 2016

State events file is packaged with ui.router so you can do following.

import uiRouter from 'angular-ui-router';
import 'angular-ui-router/release/stateEvents';

 angular
  .module('mario', [
    uiRouter,
    'ui.router.state.events',
  ])

@amir-s
Copy link

amir-s commented Dec 9, 2016

@christopherthielen Is there anyway to unhook?

Say in a controller, I hook to $transitions.onSuccess, I need to unhook from it when the component gets destroyed. Is it possible?

Update
I just found out the hook itself returns an unhook (deregister) function :)
so,

let unhook = $transitions.onSuccess({}, () => {})

unhook();

@DustinJSilk
Copy link

If only the docs were a little more simple I wouldn't have to spend a few days studying every aspect of the complicated $transition to ultimately figure out how to just grab one parameter before switching states. ugh

@ehsankhfr
Copy link

Maybe useful for any other user facing with the same issue:

https://ui-router.github.io/guide/ng1/migrate-to-1_0

@metakermit
Copy link

metakermit commented Feb 19, 2018

Here's a complete example of how to use new $transitions.

sulphur added a commit to sulphur/w11k-angular-seo-header that referenced this issue Oct 18, 2019
Hi,

$rootScope.$on('$stateChangeStart') is depracated in ui-rotuer 1.0\
angular-ui/ui-router#2720
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests