刷新页面时,Angular中的stateChangeStart事件不会触发

时间:2016-06-21 08:18:38

标签: angularjs angularjs-directive angular-ui-router angularjs-events

我有一个自定义指令:

export class XHideDirective {

    static $inject = ["$rootScope"];
    static $rootScope: any;

    public static build($rootScope) {
        var directive: ng.IDirective = {
            link: (scope, element, attributes: any) => {

                var itemToHide = attributes["xHide"];

                $rootScope.$on("$stateChangeStart",
                    (event, toState) => {
                        if (toState.data && toState.data.hasOwnProperty(itemToHide)) {
                            element.hide();
                        } else {
                            element.show();
                        }
                    });
            }
        };
        return directive;
    }
}

这样做,就是当一个州拥有它时,它会隐藏页面上的所有元素,并将该指令设置为该值。

            .state("deposit.x.successful", {
                url: "/successful/:transactionId",
                controller: "DepositResultController",
                templateUrl: "deposit/templates/x/successful.html",
                data: { hideDepositMenu: null }
            })
            .state("deposit.x.pending", {
                url: "/pending",
                templateUrl: "deposit/templates/x/pending.html",
                data: { hideDepositMenu: null }
            })
            .state("deposit.x.rejected", {
                url: "/rejected",
                templateUrl: "deposit/templates/x/rejected.html",
                data: { hideDepositMenu: null }

这一切都很有效,除非我没有自然地过渡到那个页面但我转发到那里(服务器重定向)或者我用Ctrl + F5刷新页面。然后“stateChangeStart”事件不会被击中。

该指令的注册如下:

  module Utils {
    angular.module("Utils", [])
        .directive("xHide", ["$rootScope", (r) => { return XHideDirective.build(r); }]);
     }

在这些情况下如何获得状态?

我发现这个非常相似的问题没有解决方案 $stateChangeStart don't work when user refresh browser

2 个答案:

答案 0 :(得分:0)

您是否尝试将此侦听器放入.run部分?

 $rootScope.$on("$stateChangeStart",
                    (event, toState) => {
                        if (toState.data && toState.data.hasOwnProperty(itemToHide)) {
                            element.hide();
                        } else {
                            element.show();
                        }
                    });

答案 1 :(得分:0)

我想通过这样做解决了这个问题:

export class XHideDirective {

    static $inject = ["$rootScope", "$timeout"];
    static $rootScope: any;

    public static build($rootScope, $timeout) {
        var directive: ng.IDirective = {
            controller: DepositController,
            link: (scope, element, attributes: any) => {

                var itemToHide = attributes["xHide"];

                $timeout(() => {
                    if (scope.hideMenu && scope.hideMenu.hasOwnProperty(itemToHide)) {
                        element.hide();
                    } else {
                        element.show();
                    }
                }, 0);

                $rootScope.$on("$stateChangeStart",
                    (event, toState) => {
                        if (toState.data && toState.data.hasOwnProperty(itemToHide)) {
                            element.hide();
                        } else {
                            element.show();
                        }
                    });
            }
        };
        return directive;
    }
}

然后在控制器内部:

module Deposit {
    export class DepositController extends  Utils.BaseController {  
        constructor(public $state) {
            this.$scope.hideMenu = this.$state.$current.data;
        }
    }
}

不知道这是否是最佳解决方案,但到目前为止似乎运作良好。 希望它可以帮助某人。

相关问题