title: How to remove previously provided optional param when changing state with ui-router
body: <p>I have a state with an optional query parameter.  I thought it was working fine, but I discovered that once I specify the optional parameter, it is retained from that point on, as I navigate to the same state with other parameters, unless the optional parameter is explicitly removed.</p>
  
  <p>Is there a way to make sure the optional parameter is remove between state changes?</p>
  
  <p>This is an example of how I want to change state (test a and test b), but it turns out I have to use the 'test d' version because of the retention of the optional parameter if it has been used previously.  </p>
  
  <pre><code>&lt;a ui-sref="test({req: 'a'})"&gt;test a&lt;/a&gt;&lt;br/&gt;
  &lt;a ui-sref="test({req: 'b'})"&gt;test b&lt;/a&gt;&lt;br/&gt;
  &lt;a ui-sref="test({req: 'c', opt: 'here'})"&gt;test c and opt&lt;/a&gt;&lt;br/&gt;
  &lt;a ui-sref="test({req: 'd', opt: undefined})"&gt;test d and explicity without opt&lt;/a&gt;
  </code></pre>
  
  <p>This is similar to how my state is configured:</p>
  
  <pre><code>        $stateProvider.state('test', {
              url: '/{req}?opt',
              params: {
                  opt: {value: null, squash: true}
              },
              template: '&lt;h1&gt;req = {{req}}&lt;/h1&gt;' +
                      '&lt;h1&gt;opt = {{opt}}&lt;/h1&gt;',
              controller: function($scope, $stateParams) {
                  $scope.req = $stateParams.req; 
                  $scope.opt = $stateParams.opt;
              }
          })
  </code></pre>
  
  <p>Here is a <a href="https://jsfiddle.net/8y8Lqy5v/" rel="noreferrer" title="jsfiddle">jsfiddle</a>.  Click on the first two links, and it works as expected, but once you click on the 3rd link, and then click back on either of the first two links, you will see that the optional parameter is retained, even though it is omitted in the first two links.</p>
  
  <p>I would prefer to not have to explicitly reset the optional parameter, and have it actually work as it if is optional.</p>
  
  <p>Any ideas?  Is this how it is designed to work?  If so, is there a way to get it to work the way I want?  Or have I discovered a bug?</p>
  
  <p><strong>Update</strong></p>
  
  <p>It appears that retaining the optional param is 'as designed' based on one of the answers below.  However, I am still looking at an efficient workaround, if possible.  I transition to the state from a lot of places in my app, and 99% of them don't specify the optional parameter.  I would prefer not to have to modify all of those transitions and make them know about the optional parameter, and instead, only specify the optional parameter the 1% of the time I need it.</p>
  
  <p>I've tried to create two states "test" and a child "test.opt" and then transition to "test" normally (ie, 'test a' and 'test b'), and only transition to "test.opt" when I specify the optional parameter.  However, I have a resolve which a rest call and it needs to know if the opt parameter is set, so that doesn't work as the resolve can't access the 'opt' from the child.</p>
  
  <p><strong>Update #2</strong></p>
  
  <p>I've also tried making a duplicate of the 'test' state, named 'testopt' in which case the only difference is the ?opt parameter.  However, apparently ui-router cannot distinguish between two urls that differ only by query param, and therefore it still tries to transition to 'test' even if the opt param is part of the url.</p>