javascript - Angularjs 在history.back()上设置条件

标签 javascript angularjs routes

我目前有以下指令:

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/

相关文章:

javascript - React/Redux URL 不匹配 : When the array item number differs from the object id

javascript - 定义 AngularJS Controller 时使用数组表示法的原因

javascript - 为什么 Angularjs 检测不到我的 Controller ?

Angular 6 : Guard on all routes is not working

ruby-on-rails - 如何在Rails应用程序中正确安装github的gollum Wiki?

ruby-on-rails - url_paths - 理解它们的问题

javascript - 动态填充的大型菜单

javascript - HTML5/JavaScript : open text file, 加载到文本区域/将文本区域内容保存到文本文件

angularjs - Hot Towel Angular-Breeze 类(class) - 出现 BreezeProvider 错误

javascript - 使用 ng-class 移动按钮