我目前有以下指令:
ngModule.directive('backButton', ['$window',
function ($window) {
return {
restrict: 'A',
scope: {},
templateUrl: null,
link: function (scope, element) {
scope.comingFromIndex = function () {
...
};
scope.backToIndex = function() {
$window.history.back();
};
}
};
}
]);
在我的模板中,我这样调用返回函数:
<a data-ng-click='backToIndex()' data-ng-hide='!comingFromIndex()'>Return To The Index</a>
函数 comingFromIndex
目前尚未实现,因为我不知道如何实现。可以从索引访问此后退按钮所在的页面,也可以从其他页面访问。就我而言,我希望仅当我来自索引时才显示此后退按钮。
我不确定最好的方法是什么,有什么建议吗?
最佳答案
您需要实现像 ui-router 这样的东西作为您的路线提供者。
它将状态更改广播到 rootScope,您可以通过以下方式获取状态更改:
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
//check the "from" parameter - it will contain the url of the previous page.
});
关于javascript - Angularjs 在history.back()上设置条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30506018/