Skip to content

API docs issues #3262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
adamreisnz opened this issue Jan 13, 2017 · 3 comments
Closed

API docs issues #3262

adamreisnz opened this issue Jan 13, 2017 · 3 comments

Comments

@adamreisnz
Copy link

The example for otherwise in the docs seems to be incorrect. I tried the following:

.config(function($urlRouterProvider) {
  $urlRouterProvider.otherwise((url, router) => {
    router.stateService.go('error', {error: 'page-not-found'});
  });
})

But this resulted in an error Cannot read property 'stateService' of undefined.
I then moved the logic to the run cycle instead:

.run(function($urlRouter) {
  $urlRouter.otherwise((url, router) => {
    router.stateService.go('error', {error: 'page-not-found'});
  });
})

This led to Cannot read property 'go' of undefined.

Upon analysis of parameters, I found that a boolean value is inserted as the first parameter to the function. Changing to:

.run(function($urlRouter) {
  $urlRouter.otherwise((bool, url, router) => {
    router.stateService.go('error', {error: 'page-not-found'});
  });
})

Finally did the trick.

So not sure what that first parameter is, but it's missing from the example. In addition, the return statement in the example is superfluous.

Further, the docs here should probably state:

Returns the state that the transition is going to

E.g. to instead of from.

@aj-dev
Copy link
Contributor

aj-dev commented Jan 13, 2017

$urlRouterProvider and $urlRouter are deprecated.

Try using $urlServiceProvider.rules.otherwise((matchValue, url, router) => {});.

All parameters are optional. See here https://ui-router.github.io/ng1/docs/latest/interfaces/url.urlrulehandlerfn.html

@christopherthielen
Copy link
Contributor

christopherthielen commented Jan 13, 2017

The API for $urlRouter.otherwise and $urlRouterProvider.otherwise are not the same.

The docs for $urlRouter service (injected at runtime) are correct. For $urlRouterProvider (injected at config time), follow this chain:

injectables -> $urlRouterProvider -> $urlRouterProvider.otherwise

The reason for this is:

In angular-ui-router, we've always used .config(function($urlRouterProvider) {}) to add rules like otherwise. Those rules were passed the $injector, i.e.

$urlRouterProvider.otherwise(function ($injector, $location) {
    return '/a/valid/url';
  }));

In core ui-router, we're dropping the $injector wherever possible, but the ng1 code still should support it for backwards compatibility. Since $urlRouterProvider is only an ng1 concept, it keeps the OLD API for use in .config() blocks.

@adamreisnz
Copy link
Author

Thanks for the clarification, I'll review the doc changes and refactor my code accordingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants