A component in AngularJS 1.5 does not listen to a broadcast event from $ scope

I have a problem and I cannot 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 stateful event fires on $ transitions.onSuccess (ui-router 1.0 Beta). Here is 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] Broadcast really works. cannot broadcast on first state .go tho. when the main module is working, the first instruction: $ state.go ('1'); and I cannot detect this first .go state. Other .go states are listened to.

+4
source share
2 answers

I will point out four things:

1) '*' - glob, , . '**' . ui-router glob , .

, : , onSuccess({}, ...).

https://ui-router.imtqy.com/docs/latest/interfaces/transition.hookmatchcriteria.html

. - , true .

2) , .

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

3) , ( , onSuccess), . $state.transition && & $state.transition.promise

4) $stateChange* - , . . https://ui-router.imtqy.com/docs/latest/modules/ng1_state_events.html

1.0 . : https://ui-router.imtqy.com/guide/ng1/migrate-to-1_0

+1

, , , , . .

, $transitions, , 1.0.0.beta1 to from .

,

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

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

Source: https://habr.com/ru/post/1653281/


All Articles