加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs获取以前的路由路径

发布时间:2020-12-17 08:49:46 所属栏目:安全 来源:网络整理
导读:h1{{header}}/h1!-- This Back button has multiple option --!-- In home page it will show menu --!-- In other views it will show back link --a ng-href="{{back.url}}"{{back.text}}/adiv ng-view/div 在我的模块配置 $routeProvider. when('/',{ con
<h1>{{header}}</h1>
<!-- This Back button has multiple option -->
<!-- In home page it will show menu -->
<!-- In other views it will show back link -->
<a ng-href="{{back.url}}">{{back.text}}</a>
<div ng-view></div>

在我的模块配置

$routeProvider.
  when('/',{
    controller:HomeCtrl,templateUrl:'home.html'
  }).
  when('/menu',{
    controller:MenuCtrl,templateUrl:'menu.html'
  }).
  when('/items',{
    controller:ItemsCtrl,templateUrl:'items.html'
  }).
  otherwise({
    redirectto:'/'
  });

控制器

function HomeCtrl($scope,$rootScope){
  $rootScope.header = "Home";
  $rootScope.back = {url:'#/menu',text:'Menu'};
}

function MenuCtrl($scope,$rootScope){
  $rootScope.header = "Menu";
  $rootScope.back = {url:'#/',text:'Back'};
}

function ItemsCtrl($scope,$rootScope){
  $rootScope.header = "Items";
  $rootScope.back = {url:'#/',text:'Back'};
}

正如你可以看到在我的控制器,我有硬编码的后退按钮url和文本(实际上,我不需要文本使用图像)。这样,在某些情况下,我发现后退按钮导航不正确。我不能使用history.back()coz我的后退按钮更改为主视图中的菜单链接。

所以我的问题是如何获得以前的路由路径在控制器或更好的方式来实现这一点?

我创建了一个Plunker演示我的问题。请检查。

该替代方案还提供后退功能。

模板:

<a ng-click='back()'>Back</a>

模块:

myModule.run(function ($rootScope,$location) {

    var history = [];

    $rootScope.$on('$routeChangeSuccess',function() {
        history.push($location.$$path);
    });

    $rootScope.back = function () {
        var prevUrl = history.length > 1 ? history.splice(-2)[0] : "/";
        $location.path(prevUrl);
    };

});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读