我有一个自定义指令:
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