angular / router

The Angular 1 Component Router
MIT License
665 stars 135 forks source link

`useAsDefault` route option doesn't work when no routes were matched. #415

Open d-ph opened 8 years ago

d-ph commented 8 years ago

Angular docs link say:

The useAsDefault property on the HeroList Route Definition, indicates 
that if no other Route Definition matches the URL, then this Route Definition should be 
used by default.

but when I mark a route as useAsDefault and go to an invalid url hash-address, then nothing happens.

Steps to reproduce:

  1. Download the component router demo app from plunker
  2. Extract the zip file and start your favourite web server for the downloaded folder (e.g. php -S localhost:8080)
  3. Edit app.js and change $locationProvider.html5Mode(false); to $locationProvider.html5Mode(true);. This is not strictly required, but this is what I did.
  4. Go to localhost:8080. Result: You are correctly redirected to http://localhost:8080/#/crisis-center
  5. Go to http://localhost:8080/#/heroes. Result: the Heroes component is correctly loaded.
  6. Manually change the url to say: http://localhost:8080/#/foo-bar

Result: Nothing happens. The url now says http://localhost:8080/#/foo-bar, but the application still shows the Heroes page.

Expected result: Application redirects to the useAsDefault route, which is http://localhost:8080/#/crisis-center

More info: When I change this:

if ((isBlank(parsedUrl) || parsedUrl.path == '') && possibleMatches.length == 0) {
  return PromiseWrapper.resolve(this.generateDefault(parentComponent));
}

into this:

if ((isBlank(parsedUrl) || parsedUrl.path == '') || possibleMatches.length == 0) {
  return PromiseWrapper.resolve(this.generateDefault(parentComponent));
}

in angular_1_router.js, then it works. If I was to fix that properly though, I would put

if (possibleMatches.length == 0) {
  return PromiseWrapper.resolve(this.generateDefault(parentComponent));
}

straight after

var possibleMatches = _aux ? rules.recognizeAuxiliary(parsedUrl) : rules.recognize(parsedUrl);

. But I don't actually know, where the ts file for that source is.