3

I am using Agularjs and ui-router in my application. I need to catch all urls except "signin" & "/" . All the urls must go to a state.

For example, the urls will be like

www.example.com/   // index
www.example.com/signin   // signin page

www.example.com/cars
www.example.com/sports
www.example.com/politics

My current setup is

 .config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
    $stateProvider
      .state('index', {
        url: "/",
        templateUrl: 'partials/main.html',
        controller: 'MainCtrl'             
      })
      .state('signin', {
        url: "/signin",
        templateUrl: 'partials/signin',
        controller: 'MainCtrl'             
      })
      .state("otherwise", {
        url: "*path",
        templateUrl: 'partials/detail',
        controller: 'MainCtrl' 
       });

I also tried

$urlRouterProvider.otherwise(function($injector, $location) {
  $location.path($location.path());
})

But it doesnt load/show the page.

nish
  • 256
  • 6
  • 15

1 Answers1

2

You need to use $urlRouterProvider.otherwise and route to a defined state which i called detail for your example:

.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/detail");

    $stateProvider
      .state('index', {
        url: "/",
        templateUrl: 'partials/main.html',
        controller: 'MainCtrl'             
      })
      .state('signin', {
        url: "/signin",
        templateUrl: 'partials/signin',
        controller: 'MainCtrl'             
      })
      .state("detail", {
        url: "/detail",
        templateUrl: 'partials/detail',
        controller: 'MainCtrl' 
       });
Alp
  • 29,274
  • 27
  • 120
  • 198
  • Thanks.. but this will redirect any urls to /chat. I would like to keep the original url there itself. Is this possible ? – nish May 01 '14 at 10:30
  • 1
    good question. the `*path` in your question seems to be the right way, as shown in `http://stackoverflow.com/a/18761391/675065` – Alp May 01 '14 at 10:38
  • @nish Works for me fine. The only thing you should have to modify about @Alp's code above is changing the url from `/detail` to `*path`. – Martyn Chamberlin Feb 09 '15 at 19:20