javascript UI 路由器更改状态而不更改 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27299311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:17:42  来源:igfitidea点击:

UI-router change state without changing url

javascriptangularjsangular-ui-router

提问by klmdb

Does anybody know how to change the ui-router state without changing the url? As the code below shows; in some cases the user needs to be redirected to 403 or 401 states. I would like to be able to do this redirect without changing the url.

有人知道如何在不更改 url 的情况下更改 ui-router 状态吗?如以下代码所示;在某些情况下,用户需要重定向到 403 或 401 状态。我希望能够在不更改 url 的情况下执行此重定向。

Regards, klmdb

问候, klmdb

// make sure authGetCurrent has ran before routing starts
$rootScope.$on("$locationChangeSuccess", function(event, next) {

    event.preventDefault();

    AuthService.loadCurrentAuth().then(function(){

        $urlRouter.sync();
    }, function(){

        console.log("BIG ERROR!!!");
    });
});
// Configures $urlRouter's listener *after* your custom listener
$urlRouter.listen();




$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {

    var requiredLogin       = (toState && toState.data ? toState.data.requiredLogin       : false ),
        requiredGroupRights = (toState && toState.data ? toState.data.requiredGroupRights : false );        // require the user to have at least one of these rights in the current group

    if (requiredLogin && !AuthService.isLoggedIn()) {

        event.preventDefault();
        $state.transitionTo('401');
        return;
    }
    if(requiredGroupRights){

        var i,
            hasRight = false;
        for(i=0;i<requiredGroupRights.length;i++){

            if(GroupService.checkGroupRights(toParams.groupId, requiredGroupRights[i])){
                hasRight = true;
                break;
            }
        }

        if(!hasRight){

            event.preventDefault();
            $state.transitionTo('403');
            return;
        }

    }

});

回答by Chris T

Pass the { location: false }option to $state.go

{ location: false }选项传递给 $state.go

$state.go("home.foo", {}, { location: false } );

See it in action: http://plnkr.co/edit/w2aolrt9wdW3EFcEB3Lw?p=preview

看到它在行动:http: //plnkr.co/edit/w2aolrt9wdW3EFcEB3Lw?p=preview

var app = angular.module('demonstrateissue', ['ui.router']);

app.config(function($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise("/home");
  $stateProvider.state({ 
    name: 'home', 
    url: '/home', 
    controller: function() { }, 
    template: '<h1>Home</h1><div ui-view></div>'}
  );
  $stateProvider.state({ 
    name: 'home.foo', 
    url: '/foo', 
    controller: function() { }, 
    template: '<h1>foo</h1>'}
  );
});

// Adds state change hooks; logs to console.
app.run(function($rootScope, $state, $location) {
  $rootScope.$state = $state;
  $rootScope.$location = $location;

  // This function will go to home.foo state but not change url
  $rootScope.gotofoo = function() { 
    $state.go("home.foo", {}, { location: false } );
  };
});
<!DOCTYPE html>
<html>
  <head>
    <script data-require="angular.js@*" data-semver="1.2.25" src="https://code.angularjs.org/1.2.25/angular.js"></script>
    <script data-require="ui-router@*" data-semver="0.2.13" src="https://rawgit.com/angular-ui/ui-router/0.2.13/release/angular-ui-router.js"></script>
  </head>

  <body ng-app="demonstrateissue">

      <div ui-view>/div>  

      <div class="header">
        Current URL: <b>{{$location.url()  }}</b> <br>
        Current State: <b>{{$state.current.name }}</b> <br>
        Current Params: <b>{{$state.params | json }}</b><br>
      </div>

      <!-- click this -->
      <a href ng-click="gotofoo()">Go to foo dont change url</a>
  </body>
</html>

回答by Alex Oliveira

If you want not create a function, you can pass a object to the attribute "ui-sref-opts" like following:

如果不想创建函数,可以将对象传递给属性“ui-sref-opts”,如下所示:

<a ui-sref="my-page" ui-sref-opts="{location:false}">My Page</a>

The following line (present in angular-ui-router.js) show the allowed attributes:

以下行(出现在 angular-ui-router.js 中)显示了允许的属性:

var allowedOptions = ['location', 'inherit', 'reload', 'absolute'];