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.

1 Answer 1

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' 
       });
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks.. but this will redirect any urls to /chat. I would like to keep the original url there itself. Is this possible ?
good question. the *path in your question seems to be the right way, as shown in http://stackoverflow.com/a/18761391/675065
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.