You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
fix($location): always resolve relative links in html5mode to <base> url
BREAKING CHANGE (since 1.2.0 and 1.3.0-beta.1):
Angular now requires a `<base>` tag when html5 mode of `$location` is enabled. Reasoning:
Using html5 mode without a `<base href="...">` tag makes relative links for images, links, ...
relative to the current url if the browser supports
the history API. However, if the browser does not support the history API Angular falls back to using the `#`,
and then all those relative links would be broken.
The `<base>` tag is also needed when a deep url is loaded from the server, e.g. `http://server/some/page/url`.
In that case, Angular needs to decide which part of the url is the base of the application, and which part
is path inside of the application.
To summarize: Now all relative links are always relative to the `<base>` tag.
Exception (also a breaking change):
Link tags whose `href` attribute starts with a `#` will only change the hash of the url, but nothing else
(e.g. `<a href="#someAnchor">`). This is to make it easy to scroll to anchors inside a document.
Related to #6162Closes#8492
BREAKING CHANGE (since 1.2.17 and 1.3.0-beta.10):
In html5 mode without a `<base>` tag on older browser that don't support the history API
relative paths were adding up. E.g. clicking on `<a href="page1">` and then on `<a href="page2">`
would produce `$location.path()==='/page1/page2'. The code that introduced this behavior was removed
and Angular now also requires a `<base>` tag to be present when using html5 mode.
Closes#8172, #8233
@fullName $location in HTML5 mode requires a <base> tag to be present!
4
+
@description
5
+
6
+
If you configure {@link ng.$location `$location`} to use
7
+
{@ng.provider.$locationProvider `html5Mode`} (`history.pushState`), you need to specify the base URL for the application with a [`<base href="">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) tag.
8
+
9
+
The base URL is then used to resolve all relative URLs throughout the application regardless of the
10
+
entry point into the app.
11
+
12
+
If you are deploying your app into the root context (e.g. `https://myapp.com/`), set the base URL to `/`:
13
+
14
+
```html
15
+
<head>
16
+
<base href="/">
17
+
...
18
+
</head>
19
+
```
20
+
21
+
If you are deploying your app into a sub-context (e.g. `https://myapp.com/subapp/`), set the base URL to the
22
+
URL of the subcontext:
23
+
24
+
```html
25
+
<head>
26
+
<base href="/myapp">
27
+
...
28
+
</head>
29
+
```
30
+
31
+
Before Angular 1.3 we didn't have this hard requirement and it was easy to write apps that worked
32
+
when deployed in the root context but were broken when moved to a sub-context because in the
33
+
sub-context all absolute urls would resolve to the root context of the app. To prevent this,
34
+
use relative URLs throughout your app:
35
+
36
+
```html
37
+
<!-- wrong: -->
38
+
<a href="/userProfile">User Profile</a>
39
+
40
+
41
+
<!-- correct: -->
42
+
<a href="userProfile">User Profile</a>
43
+
44
+
```
45
+
46
+
Additionally, if you want to support [browsers that don't have the `history.pushState`
47
+
API](http://caniuse.com/#feat=history), the fallback mechanism provided by `$location`
48
+
won't work well without specifying the base url of the application.
49
+
50
+
In order to make it easier to migrate from hashbang mode to html5 mode, we require that the base
51
+
URL is always specified when `$location`'s `html5mode` is enabled.
0 commit comments