1

我有一个自定义指令:

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

4

2 回答 2

0

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

 $rootScope.$on("$stateChangeStart",
                    (event, toState) => {
                        if (toState.data && toState.data.hasOwnProperty(itemToHide)) {
                            element.hide();
                        } else {
                            element.show();
                        }
                    });
于 2016-06-21T09:56:58.563 回答
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;
        }
    }
}

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

于 2016-06-21T15:45:42.643 回答