UI-router change state without changing url

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.

Regards, 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;
        }

    }

});
Chris T

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

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

See it in action: 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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular ui-router: Can you change state without changing URL?

From Java

AngularJS UI Router - change url without reloading state

From Dev

UI-Router: Change url without loading state or defer loading the state?

From Dev

Using ui-router to simply launch a function without changing state

From Dev

Using ui-router to simply launch a function without changing state

From Dev

URL is not changing even after the state is changed . `ui-router`

From Dev

UI-Router - Change $state without rerender/reload of the page

From Dev

AngularJS ui-router default state for states without a URL

From Dev

Cannot change state from url bar with ui-router

From Dev

UI Router changing URL but not page

From Dev

Change ui-view template without changing URL

From Dev

ui-router - stop changing state on resolve

From Dev

ui-router changing the previous state

From Dev

AngularJS ui-router $state.go('^') only changing URL in address bar, but not loading controller

From Dev

AngularJS ui-router $state.go('^') only changing URL in address bar, but not loading controller

From Dev

Change a url parameter in the current state from ui-sref in angular-ui-router

From Dev

Change a url parameter in the current state from ui-sref in angular-ui-router

From Dev

Ui-router switching back to home state (url:"") doesn't change the url

From Dev

How to change only one ui-view out of many. Without changing state

From Dev

How to change only one ui-view out of many. Without changing state

From Dev

UI Router State Change Tracking in Google Analytics

From Dev

Angular ui-router change state on scroll

From Dev

UI-Router - scope not destroyed on state change?

From Dev

Updating parent scope on UI router state change

From Dev

AngularJS ui-router state change

From Dev

UI Router 1.0 state change events not working

From Dev

UI-router abstract state with no url

From Dev

AngularJS / UI Router - Locale in State URL / TemplateURL

From Dev

UI Router fails to update URL in address bar after successful state change

Related Related

  1. 1

    Angular ui-router: Can you change state without changing URL?

  2. 2

    AngularJS UI Router - change url without reloading state

  3. 3

    UI-Router: Change url without loading state or defer loading the state?

  4. 4

    Using ui-router to simply launch a function without changing state

  5. 5

    Using ui-router to simply launch a function without changing state

  6. 6

    URL is not changing even after the state is changed . `ui-router`

  7. 7

    UI-Router - Change $state without rerender/reload of the page

  8. 8

    AngularJS ui-router default state for states without a URL

  9. 9

    Cannot change state from url bar with ui-router

  10. 10

    UI Router changing URL but not page

  11. 11

    Change ui-view template without changing URL

  12. 12

    ui-router - stop changing state on resolve

  13. 13

    ui-router changing the previous state

  14. 14

    AngularJS ui-router $state.go('^') only changing URL in address bar, but not loading controller

  15. 15

    AngularJS ui-router $state.go('^') only changing URL in address bar, but not loading controller

  16. 16

    Change a url parameter in the current state from ui-sref in angular-ui-router

  17. 17

    Change a url parameter in the current state from ui-sref in angular-ui-router

  18. 18

    Ui-router switching back to home state (url:"") doesn't change the url

  19. 19

    How to change only one ui-view out of many. Without changing state

  20. 20

    How to change only one ui-view out of many. Without changing state

  21. 21

    UI Router State Change Tracking in Google Analytics

  22. 22

    Angular ui-router change state on scroll

  23. 23

    UI-Router - scope not destroyed on state change?

  24. 24

    Updating parent scope on UI router state change

  25. 25

    AngularJS ui-router state change

  26. 26

    UI Router 1.0 state change events not working

  27. 27

    UI-router abstract state with no url

  28. 28

    AngularJS / UI Router - Locale in State URL / TemplateURL

  29. 29

    UI Router fails to update URL in address bar after successful state change

HotTag

Archive