Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Routing redirection renders an incorrect view at first load #10041

Closed
@ghusse

Description

@ghusse

Reproducted on angular 1.3.*, but not on 1.2.26.

Steps to reproduce, with this code (jsbin):

  1. Load the page
  2. You are redirected to /#/login -> OK
  3. ... but the displayed content comes from another view (called needsAuth) -> KO
  4. Reload
  5. The URI ends with /#/login -> OK
  6. The content comes from the login view -> OK

On First load:
firstload

After a reload:
afterreload

HTML Code:

<!DOCTYPE html>
<html ng-app='app'>
<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div ng-view>
  </div>
</body>
</html>

Javascript code

var template = '{{vm.text}}';

angular
  .module('app', ['ngRoute'])
  .config(['$routeProvider', routes])
  .controller('needsAuthController', needsAuthController)
  .controller('loginController', loginController)
  .run(['$rootScope', '$location', registerRedirection]);

function routes($routeProvider){
  $routeProvider
    .when('/needsAuth', {
      controller: 'needsAuthController',
      controllerAs: 'vm',
      template: template
    })
    .when('/login', {
      controller: 'loginController',
      controllerAs: 'vm',
      template: template
    })
    .otherwise({
      redirectTo: '/needsAuth'
    });
}

function needsAuthController(){
  var vm = this;

  vm.text = 'needsAuth';
}

function loginController(){
  var vm = this;

  vm.text = 'login';
}

function registerRedirection($rootScope, $location){
  $rootScope.$on('$routeChangeStart', function(event, next){
    $location.path('/login');
  });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions