Using $routeProvider to redirect to routes outside of Angular

后端 未结 5 685
难免孤独
难免孤独 2020-12-05 04:34

I\'ve written part of a web application in Angular. To ensure that all routes are covered, I wanted to add a redirectTo property to the $routeProvider

相关标签:
5条回答
  • 2020-12-05 04:41

    The above solution with /404 does not work for me. This however seems to work

    .otherwise({
        controller : function(){
            window.location.replace('/');
        }, 
        template : "<div></div>"
    });
    

    PS. I am using Angular 1.2.10

    0 讨论(0)
  • 2020-12-05 04:43

    You could do something like this:

    $routeProvider.when('/404', {
        controller: ['$location', function($location){
            $location.replace('/');
        }]
    }).otherwise({
        redirectTo: '/404'
    });
    

    It is essentially the same thing, only it uses less code.

    0 讨论(0)
  • 2020-12-05 04:45

    Not sure what version of Angular JS the accepted answer was written on, but 'redirectTo' property takes in a function. So, why not do something simpler like this:

    $routeProvider.otherwise({
        redirectTo: function() {
            window.location = "/404.html";
        }
    });
    

    Obviously, you have to create your own 404.html. Or wherever your 404 page is.

    0 讨论(0)
  • 2020-12-05 04:50

    Hi even though it's been two years, just for some one who search for this answer, simply use window.location.assign('/login'). It's work for me.

    0 讨论(0)
  • 2020-12-05 04:55

    None of the answers including the marked answer worked for me. I believe my solution solves your problem as well and I'd share my use-case as well for future readers' reference.

    Issue with using the route controller method: When the controller is loaded the routing already have accessed the History API states for me (I use HTML5 mode, not sure whether this affects non-HTML5 mode).

    As a result, even though I can forward people to the correct page using window.location.replace('/');, if the user then clicks Back on their browser, it goes to invalid state.

    Scenario: We implement multi-page model and I have my admin page module separate from my homepage (non-admin) modules. I have a $location.path('/') somewhere in one of my admin controller, but since homepage isn't packaged into the admin page module, I want to force full page reload when I detect the '/' route.

    Solution: We have to intercept at the $routeChangeStart before ngRoute accesses any of the state info. This way we can even specify external href by passing url to redirectTo param in the $route

    angular.module('app',['ngRoute'])
    .config(['$routeProvider', function($routeProvider) {
      $routeProvider
      .when('/admin/default', {template: somePageTemplate})
      /*
       *  More admin-related routes here...
       */
      .when('/',{redirectTo:'/homepage'})  // <-- We want to intercept this
      .otherwise({redirectTo: '/admin/default'}); 
    }])
    .controller('MainCtrl',[ // <- Use this controller outside of the ng-view!
      '$rootScope','$window',
      function($rootScope,$window){
        $rootScope.$on("$routeChangeStart", function (event, next, current) {
          // next <- might not be set when first load
          // next.$$route <- might not be set when routed through 'otherwise'
          // You can use regex to match if you have more complexed path...
          if (next && next.$$route && next.$$route.originalPath === '/') {
            // Stops the ngRoute to proceed
            event.preventDefault();
            // We have to do it async so that the route callback 
            // can be cleanly completed first, so $timeout works too
            $rootScope.$evalAsync(function() {
              // next.$$route.redirectTo would equal be '/homepage'
              $window.location.href = next.$$route.redirectTo;
            });
          }
        });
      }
    ]);
    

    Please provide any feedback as I will be using this code myself. Cheers

    Reference: https://github.com/angular/angular.js/issues/9607

    0 讨论(0)
提交回复
热议问题