2

I am trying to define a default route when angularjs hits the below url

http://localhost:62831/#/

The default route should go to home page below.

http://localhost:62831/

How can I define it using $urlRouterProvider ? The below line does not seem to be making any difference.

$urlRouterProvider.otherwise('/');

Any suggestions? Thank you!

 appl.config(['$stateProvider', '$urlMatcherFactoryProvider', '$locationProvider', '$urlRouterProvider',
    function ($stateProvider, $urlMatcherFactoryProvider, $locationProvider, $urlRouterProvider) {

        $urlMatcherFactoryProvider.caseInsensitive(true);

        $stateProvider
            .state('home', {
                url: '',
                templateUrl: 'search',
                controller: 'applCtrl'
            })
            .state('person', {
                url: '/person',
                templateUrl: 'Person',
                controller: 'applPersonCtrl'
            })

        $urlRouterProvider.otherwise('/');

        $locationProvider.hashPrefix(''); //it defaults to ! after angularjs 1.5

    }]);
2
  • any console errors? Commented Jan 7, 2019 at 22:43
  • What happens if you set <base href="/"> inside your <head></head> element in index.html and then add $locationProvider.html5Mode(true); just above where you're calling hashPrefix('')? Commented Jan 7, 2019 at 23:28

1 Answer 1

1

You can match on the url of your site (with trailing /) and explicitly go to the home state as follows, while also having a not found url condition:

$urlRouterProvider.otherwise(function ($injector, $location) {
    var $state = $injector.get('$state');
    if ($location.$$url === '' || $location.$$url === '/') {
        $state.go('home');
        return;
    }
    $state.go('notFound'); // some custom not found page
});
Sign up to request clarification or add additional context in comments.

Comments

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.