Default route for angular ui router

后端 未结 6 2341
死守一世寂寞
死守一世寂寞 2020-12-15 18:58

The sample demo of the angular ui router has this link for the start page:

full url of \'ui-router\' is / or http://angular-ui.github.io/ui-route

相关标签:
6条回答
  • 2020-12-15 19:24

    See here there is an "otherwise" option for a default route.

    If you are talking about default route PARAMETERS, then there is an answer here.

    0 讨论(0)
  • 2020-12-15 19:33

    The simplest way of all

    add $urlRouterProvider service in config(function($urlRouterProvider))

    and then

    app.config(function ($stateProvider, $urlMatcherFactoryProvider, $urlRouterProvider) {
    
             $urlRouterProvider.otherwise('employeesState'); //default route
    
             $stateProvider.state('employeesState', function(){
                  //state configuration here
             }) //employees state
    
             $stateProvider.state('cutomersState', function(){
                 //state configuration here
             })
    }
    

    name any of the state in otherwise function to which you want as your default state/route

    0 讨论(0)
  • 2020-12-15 19:35

    use the $urlRouterProvider and its otherwise function:

    angular.module('MyApp', ['ui.router'])
      .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
         $stateProvider.state('default', {
         controller: 'MyController',
         templateUrl: 'path/to/template.ng.html',
         url:'/default'
        })
        $urlRouterProvider.otherwise('/default')
      })];
    
    0 讨论(0)
  • 2020-12-15 19:38

    The default route for ui-router can be whatever you want it to be, there is no limitaion like in DurandalJS. Just use:

    $stateProvider
        .state("otherwise", { url : '/otherwise'...})
    

    This is the official documentaion of ui-router, however I could not find the otherwise technique in there: https://github.com/angular-ui/ui-router/wiki

    @apohi: ui-router is not angular-route. Your reply is adressing the wrong module.

    0 讨论(0)
  • 2020-12-15 19:47

    You can do it this way:

    angular.module('MyApp', ['ui.router']).config([
      '$stateProvider', function ($stateProvider) {
         $stateProvider.state('default', {
         controller: 'MyController',
         templateUrl: 'path/to/index.html',
         url:''
      })
    )];
    

    That way whenever the url is on the root of your site ui-router will capture it on a state that matches, in this case, 'default'.

    0 讨论(0)
  • 2020-12-15 19:47

    You can use $urlRouterProvide.otherwise. This will work if you try to navigate to a route url that has not been defined.

    Taken from here.

    function configurUIRoutes($stateProvider, $urlRouterProvider) {
    
    $stateProvider
        .state('myhomepage',
        {
            abstract: false,
            url: '/myhomepageurl',
            templateUrl: "some-path/staff-admin.html",
            controller: 'HomeController'
        });
    
    $urlRouterProvider.otherwise('/myhomepageurl'); // User will be taken to /myhomepageurl if they enter a non-matched entry into URL
    
    }
    
    0 讨论(0)
提交回复
热议问题