Skip to content Skip to sidebar Skip to footer

Component In Angularjs 1.5 Doesn't Listen Broadcasted Event From $scope

I'm having this issue and I can't really figure out how to solve this. I have this component: (function () { 'use strict'; // Usage: // // Creates: //

Solution 1:

I'm not sure if you are experiencing an issue similar to mine, in such case I hope my findings will help you. The full description of my problem, and the solution I found is here.

In short, I came across some issues while using $transitions, and I found out that, with version 1.0.0.beta1 the to and from params were not working.

So, instead of

$transitions.onSuccess({to: '*', form: '*'}, function(){});

I'm using

$transitions.onSuccess({}, function(){
    if($state.current.name == 'myState') // do stuff
});

Solution 2:

I'll point out four things:

1) '*' is a glob pattern which matches any state at the root level, but it doesn't match child states. Use double star '**' to match child states too. The ui-router glob documentation is scattered and not very good, sorry.

Better yet, the default to: criteria already matches any state, so use onSuccess({}, ...).

This is documented here https://ui-router.github.io/docs/latest/interfaces/transition.hookmatchcriteria.html

All properties are optional. If any property is omitted, it is replaced with the value true, and always matches.

2) If you create a hook in a controller, you should deregister that hook when the controller scope is destroyed.

var deregisterFn = $transitions.onBefore(...)
$scope.$on('$destroy', deregisterFn);

3) If a transition is in progress before your controller is initialized (and before your onSuccess hook registers), the initial transition won't be captured. You can hook into the in-progress transition promise from $state.transition && $state.transition.promise

4) The legacy $stateChange* events are still available, but you have to opt-in. See https://ui-router.github.io/docs/latest/modules/ng1_state_events.html

For more information about migrating to 1.0, see this guide: https://ui-router.github.io/guide/ng1/migrate-to-1_0

Post a Comment for "Component In Angularjs 1.5 Doesn't Listen Broadcasted Event From $scope"