简体   繁体   中英

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:
    // 

    myApp
        .component('navbar', {
            //template:'htmlTemplate',
            templateUrl: 'app/components/navbar/navbar.partial.html',
            controller: ControllerController,
            bindings: {
                progress: '<'
            },
        });

    ControllerController.$inject = ['$scope','$rootScope','changeState'];
    function ControllerController($scope,$rootScope,changeState) {

        var $ctrl = this;

        $scope.$on('state-changed',function(event,args){
            console.log(args);
        });




        $ctrl.$onInit = function () { };
        $ctrl.$onChanges = function (changesObj) { };
        $ctrl.$onDestory = function () { };
    }
})();

The event 'state-changed' is triggered on $transitions.onSuccess (ui-router 1.0 Beta). Here the code:

var myApp = angular.module('myApp', ['ui.router']);

myApp.controller('appCtrl', ['$scope', '$state', 'formDataService', '$timeout', '$transitions','changeState','$transitions',
        function ($scope, $state, formDataService, $timeout, $transitions,changeState,$transitions) {

        changeState.go('1');
        $scope.stateByFar = 1;

        $scope.currentState = function(){
            return $state.current.name;
        };

        $scope.updateStateByFar = function(){
            if (parseInt($scope.currentState())>$scope.stateByFar)
                $scope.stateByFar=parseInt($scope.currentState());
        };



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

                $scope.updateStateByFar();
                console.log($scope.stateByFar);
                $scope.$broadcast('state-changed',{currentState: $scope.currentState(),
                                                    stateByFar : $scope.stateByFar});

            }
            );




    }]);

[EDIT] The broadcast actually works. can't broadcast on the first state.go tho. when the main module runs, the first instruction is : $state.go('1'); and I can't detect this first state.go. Further state.go are listened.

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
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM