B
B
byms2017-05-05 12:43:55
Angular
byms, 2017-05-05 12:43:55

Why does it duplicate events on state.reload?

export class AppController {
    constructor(Guard, $state, $rootScope, $injector) {
        'ngInject';

        this.guard = Guard;
        this.state = $state;
        this.rootScope = $rootScope;
        this.init();
    }

    init() {
        this.pageTitle = this.state.current.ncyBreadcrumb.label;
        this.help(this.state.current.url);

        this.rootScope.$on('$locationChangeStart', (event, toState, toParams, fromState, fromParams) => {
            this.guard.loadUser();
        });

        this.rootScope.$on('$stateChangeSuccess', (event, toState, toParams, fromState, fromParams) => {
            console.log(1);
        });
    }
}

there are such events, when this.state.go(state, {}, { reload: true }) is called, the events start to be duplicated, i.e. this.guard.loadUser() and console.log(1) will work 2 times.
"angular": "^1.5.8",
"angular-ui-router": "0.4.2",

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nicholas, 2017-05-05
@healqq

I suspect that with each initialization, a new handler is hung up, and the old one is not deleted. Events need to be monitored. Why is this in the controller at all?

O
ozknemoy, 2017-05-08
@ozknemoy

better move this.rootScope.$on to config

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question