Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I modify the url that is constructed from ui-router?
    primarykey
    data
    text
    <p>I'm currently using ui-router to manage my routes in my angularjs app, and I was wondering if there was a way to modify the url that gets created.</p> <p>Its better explained with an example. I have the following state</p> <pre><code>$stateProvider.state('items', { url: 'items/{itemName}', ... }); </code></pre> <p>Then in my view I can transition to this state with </p> <pre><code>&lt;a ui-sref="items({itemName:'Full Item Name'})"&gt;...&lt;/a&gt; </code></pre> <p>which, when clicked, to set the url to <code>/items/Full%20Item%20Name</code> What I would like to do is modify the url that will be generated to <code>/items/full-item-name</code>, but still have <code>Full Item Name</code> show up in the $stateParams.</p> <p>I have managed to change the navigated url to what I wanted using</p> <pre><code>$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { for (var prop in toParams) { var original = toParams[prop]; var pretty = makePretty(original); toParams[prop] = pretty; } }); </code></pre> <p>This will change the toParams to their 'prettified' form before the route change happens so that the url will be what I want</p> <p>Here is a plunk: <a href="http://plnkr.co/edit/8AhkitxFjy7H6ZwTkeT4?p=preview" rel="nofollow">http://plnkr.co/edit/8AhkitxFjy7H6ZwTkeT4?p=preview</a></p> <p>The main thing that I am missing right now is the url that is pointed to by the link is still <code>items/Full%20Item%20Name</code>.</p> <p>Am I missing something? Is there an easier way to approach this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload