Alan2
Alan2

Reputation: 24562

How can I find out if a route has been specified when my AngularJS ui-router app starts up

I have an AngularJS SPA application that uses angular-ur-router. When the application starts normally it is when the user entered:

(a) www.example.com 

and then the index.html page is called automatically. At this time I would like here is for the application to go to the home state but I am not sure how to detect condition (a).

When a user enters:

(b) www.example.com/access
(b) www.example.com/example
(b) www.example.com/something/else 

then the route is found and it goes to the correct state

How can I check if the route entered is like (a) or like (b) when the application starts up and if (a) then redirect to the home state

I already know that I can use: $state.go('home'); to change to the state I want but I am not sure how to detect if there is or is not a route specified on start up.

Upvotes: 1

Views: 51

Answers (1)

Shashank Agrawal
Shashank Agrawal

Reputation: 25797

You can test with

$state.is("home.access");

You need to modify your state configuration:

myApp.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {

    // If no state is provided on page load, take me to /access or other state
    $urlRouterProvider
        .otherwise('/access');

    // other state config

    $stateProvider.state("home", {
        url: '/'   // this will be treated as the state when you page load at www.example.com
        // add other properties
    })

    $stateProvider.state("home.access", {
        url: "/access"
        // just an example
    })
}]);

Upvotes: 1

Related Questions