Use "/" as an optional delimiter for parameters in the URL - Angular-Ui-Router

I have a difficult problem that I could not solve for some time.

Essentially, when I am on a specific $routeone and I am doing one $state.go(routeName, params);, I need to add one parameteror more parametersto the URL.

Also, paying attention to the fact that the route can also be visited without any parameters, so it should be clean.

I would like to achieve this result:

  • The actual address: localhost:4000
  • Required Address: localhost:/4000/myRoute/myStuff/myParam
  • Avoid: localhost:/4000/myRoute//if specified $state.go(myRoute)without parameters.

For instance:

$state.go(myRoute, {
   param1 : 'myStuff',
   param2 : 'myParam'
});

function myRoute($stateProvider) {

    $stateProvider
        .state('myRoute', {
            url: 'myRoute/{param1}{param2}',
            parent: 'myParent'
              . . .
        })
}

Will work, but gives localhost:/4000/myRoute/myStuffmyParam.

So, I tried to use /as a separator url: 'myRoute/{param1}' + '/' + '{param2}' and it works fine, I get localhost:/4000/myRoute/myStuff/myParam.

, , / , . , $state.go('myRoute') localhost:/4000/myRoute//, .

, , , , param1, , .

,

  • param1 : 'myStuff' + '/'
  • param1 : 'myStuff' + '\/'
  • param1 : 'myStuff/'
  • param1 : 'myStuff\/'

URL localhost:4000/myRoute/myStuff~2FmyParam. , , -, .

param1 : 'myStuff-' localhost:4000/myRoute/myStuff-myParam

{param} :param - . angular -ui/ui-router URL-

, URL-?. myRoute[/:param1[/:param2]] .

param1%2F 'param1' + '%2F' . ~2F %252f . decodeURI('%2F') .

  • , , , ?

. .

+4
3

, . @trichetriche, .

$routeProvider :

//for the clean url - localhost:/4000/myRoute/
$stateProvider
    .state('myRoute', {
        url: 'myRoute/',
        parent: 'myParent'
          . . .
})
//for myStuff/param1 url - localhost:/4000/myStuff/param1
$stateProvider
    .state('myRoute.myStuff', {
        url: 'myStuff/{param1}', //also adding other params
        parent: 'myRoute'
          . . .
})

, , .

$state.go's:

  • $state.go('myRoute');
  • $state.go('myRoute.myStuff', { param1 : 'HeyOhLetsGo' });
+1
$stateProvider.state('state', {
    url: "/state/:id/:name/:something",
    templateUrl: "bla.html",
    controller: 'BlaCtrl'
  });

$state.go('state', {id: 1, name: 'pepe', something: 'nothing'});

, : /state/1/pepe/nothing

+3

Can you create an obscene route and a children's route? Is it impossible?

+1
source

Source: https://habr.com/ru/post/1660796/


All Articles