question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Do not annotate ui-router's $urlRouterProvider.otherwise

See original GitHub issue

Currently, this:

$urlRouterProvider.otherwise(function ($injector) {
  //...
});

is being annotated into:

$urlRouterProvider.otherwise(["$injector", function ($injector) {
  //...
}]);

But ui-router expects a function:

this.otherwise =
    function (rule) {
      if (isString(rule)) {
        var redirect = rule;
        rule = function () { return redirect; };
      }
      else if (!isFunction(rule)) throw new Error("'rule' must be a function");
      otherwise = rule;
      return this;
    };

and already passes $injector as a parameter to otherwise:

function update() {
        function check(rule) {
          var handled = rule($injector, $location);
          if (handled) {
            if (isString(handled)) $location.replace().url(handled);
            return true;
          }
          return false;
        }
        var n=rules.length, i;
        for (i=0; i<n; i++) {
          if (check(rules[i])) return;
        }
        // always check otherwise last to allow dynamic updates to the set of rules
        if (otherwise) check(otherwise);
      }

I’m not being able to keep ng-annotate from annotating this by using:

regexp: /^((?!otherwise).)*$/

I probably misunderstood the regexp option, though.

Workaround:

var urlRouterProvider = $urlRouterProvider;
urlRouterProvider.otherwise(function ($injector) {
  //...
});

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gabrielmaldicommented, May 11, 2014

I took a look at ui-router’s source and then ran a few tests.

Takeaways:

  • Only when can handle an array with annotations and a function, i.e. this is OK:
$urlRouterProvider.when("someRoute", ['$match', '$stateParams', function ($match, $stateParams) {
  //...
}]);

Proof:

this.when =
    function (what, handler) {
      var redirect, handlerIsString = isString(handler);
      if (isString(what)) what = $urlMatcherFactory.compile(what);

      if (!handlerIsString && !isFunction(handler) && !isArray(handler))
        throw new Error("invalid 'handler' in when()");
  • Neither rule nor otherwise will accept an array and will throw if that’s the case:
this.rule =
    function (rule) {
      if (!isFunction(rule)) throw new Error("'rule' must be a function");
this.otherwise =
    function (rule) {
      if (isString(rule)) {
        var redirect = rule;
        rule = function () { return redirect; };
      }
      else if (!isFunction(rule)) throw new Error("'rule' must be a function");
  • $urlRouterProvider’s only public (and documented) functions are when, rule, and otherwise; so perhaps including anythingreally might bite us if new functions which can’t be annotated are added to ui-router in the future (but it is entirely up to you to make this call 😃).
0reactions
stodoloscommented, Mar 5, 2016

Never mind… I had two “config” functions (one for my module config and another from a directive’s config, which came from a template I purchased), and I was blaming the module config the whole time. I finally came to realization when I enabled ng-strict-di. That clearly told me the name of the piece causing the issue, and then I was able to narrow it down.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ui-router dosn't work with $urlRouterProvider.otherwise
I tried this before my post in stackoverflow, but don't work. – dani. Jan 31, 2018 at 8:12. Add a comment ...
Read more >
Routing in Angular JS using Angular UI Router - GeeksforGeeks
Angular-UI-Router has stateProvider method which is used to create routes/states in application. State Provider takes state name and state ...
Read more >
ui-router/CHANGELOG.md at master · angular-ui/ui ... - GitHub
The de-facto solution to flexible routing with nested views in AngularJS - ui-router/CHANGELOG.md at master · angular-ui/ui-router.
Read more >
From ui-router to Component Router - Telerik Blogs
otherwise to set up a fallback if a valid route is not found. function config($stateProvider, $urlRouterProvider) { $stateProvider ...
Read more >
Url.urlrouterprovider - UI-Router
Returns void · otherwise(rule: string | RawNg1RuleFunction): UrlRouterProvider · Defines the path or behavior to use when no url can be matched.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found