Otherwise on StateProvider

Using angular -ui-router, how can I use the method differently on $ stateProvider or how can I use it at all?

+86
angularjs angular-ui-router angular-ui
May 28 '13 at 13:46
source share
5 answers

You cannot use only $stateProvider .

You need to enter $urlRouterProvider and create code similar to:

 $urlRouterProvider.otherwise('/otherwise'); 

The /otherwise URL should be defined as usual:

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

See this link where ksperling explains

+116
Jun 11 '13 at 20:35
source share

You can use $stateProvider with the syntax catch all ( "*path" ). You just need to add the state configuration at the bottom of the list, as shown below:

 $stateProvider.state("otherwise", { url: "*path", templateUrl: "views/error-not-found.html" }); 

All parameters are described in https://github.com/angular-ui/ui-router/wiki/URL-Routing#regex-parameters .

The good thing about this parameter, unlike $urlRouterProvider.otherwise(...) , is that you are not forced to change the location.

+77
Sep 12 '13 at 10:10
source share

You can also manually enter $ state into another function, after which you can switch to non-url state. This has the advantage of a browser that does not change the address bar, which is useful when accessing the previous page.

  $urlRouterProvider.otherwise(function ($injector, $location) { var $state = $injector.get('$state'); $state.go('defaultLayout.error', { title: "Page not found", message: 'Could not find a state associated with url "'+$location.$$url+'"' }); }); 
+31
Aug 25 '14 at 0:59
source share

Well, a silly moment, when you understand that the question you have already asked already answers in the first lines of the sample code! Just take a look at the sample code.

  angular.module('sample', ['ui.compat']) .config( [ '$stateProvider', '$routeProvider', '$urlRouterProvider', function ($stateProvider, $routeProvider, $urlRouterProvider) { $urlRouterProvider .when('/c?id', '/contacts/:id') .otherwise('/'); // <-- This is what I was looking for ! .... 

Look at here.

+3
Jun 26 '13 at 12:08
source share

accepted answer angular-route asks about ui-router . The accepted answer uses "monolithic" $routeProvider , which requires the ngRoute module (whereas ui-router needs the ui.router module)

the highest rating uses $stateProvider instead and says something like .state("otherwise", { url : '/otherwise'... }) , but I cannot find the mention of β€œotherwise” in the documentation that it binds . However, I see that $stateProvider is mentioned in this answer , which says:

You cannot use only $stateProvider . You need to enter $urlRouterProvider

Where my answer can help you. For me, it was enough to use $urlRouterProvider as follows:

  my_module .config([ , '$urlRouterProvider' , function( , $urlRouterProvider){ //When the url is empty; ie what I consider to be "the default" //Then send the user to whatever state is served at the URL '/starting' //(It could say '/default' or any other path you want) $urlRouterProvider .when('', '/starting'); //... }]); 

My suggestion is consistent with the ui-router documentation ( this particular version ), where it includes a similar use of the when(...) method (first function call):

 app.config(function($urlRouterProvider){ // when there is an empty route, redirect to /index $urlRouterProvider.when('', '/index'); // You can also use regex for the match parameter $urlRouterProvider.when(/aspx/i, '/index'); }) 
0
Oct 27 '17 at 20:46 on
source share



All Articles