This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
$location.url has no effect when given an empty string #10063
Milestone
Comments
@NevilleS what you are saying sounds reasonable on the first read. If you can put together a PR with a test that reproduces your case and doesn't break existing tests there is a great chance that such PR would be accepted. But yeh, send a patch, will look into it in more details. |
Okey dokey, I'll pull something together today and send it over. |
NevilleS
added a commit
to NevilleS/angular.js
that referenced
this issue
Nov 14, 2014
…ng URL Currently, providing '' to $location#url will only reset the hash, but otherwise has no effect. This change brings the behaviour of $location#url more inline with window.location.href, which when assigned to an empty string loads the page's base href. Before: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com/path After: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com Fixes angular#10063
NevilleS
added a commit
to NevilleS/angular.js
that referenced
this issue
Nov 14, 2014
Currently, providing '' to $location#url will only reset the hash, but otherwise has no effect. This change brings the behaviour of $location#url more inline with window.location.href, which when assigned to an empty string loads the page's base href. Before: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com/path After: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com Fixes angular#10063
NevilleS
added a commit
to NevilleS/angular.js
that referenced
this issue
Nov 14, 2014
Currently, providing '' to $location#url will only reset the hash, but otherwise has no effect. This change brings the behaviour of $location#url more inline with window.location.href, which when assigned to an empty string loads the page's base href. Before: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com/path After: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com Fixes angular#10063
OK, see that PR for a very simplistic fix for this. |
Roman-Didenko
pushed a commit
to Roman-Didenko/angular.js
that referenced
this issue
Mar 31, 2015
Currently, providing '' to $location#url will only reset the hash, but otherwise has no effect. This change brings the behaviour of $location#url more inline with window.location.href, which when assigned to an empty string loads the page's base href. Before: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com/path After: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com Fixes angular#10063 Closes angular#10064
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Overview of the Issue
$location.url()
is used to update the current URL in the address bar, trigger route changes, etc. It's behaviour is analogous to directly usingwindow.location
to trigger URL changes, but is more Angular-y 😄. However, if you call$location.url('')
, nothing happens!Expected Behaviour
$location.url('')
updates address bar and$location.url()
tohttp://www.example.com
Actual Behaviour
$location.url('')
does nothing, neither the address bar nor$location.url()
are changedWorkaround
window.location.pathname = ''
orwindow.location.href = ''
does change the URL tohttp://www.example.com
as I would expect, but for a variety of (pretty obvious) reasons, I'd prefer to use$location
for this 👍Motivation for or Use Case
I am using UI router and have a state defined as my "home" whose URL is an empty string, e.g.
http://www.example.com
. Child states append to the path, like/path
,/path/other
, etc.Everything works fine from a routing perspective: navigating to
http://www.example.com
matches thehome
state with an empty path, child states update the address bar correctly (using history API on modern browsers, falling back to hashbangs on IE9), etc. However, when navigating back to thehome
state, UI router invokes$location.url
with an empty string, and the address bar isn't updated. Therefore, if the user refreshes the page for any reason, they unexpectedly move away from thehome
state to wherever they were previously...Angular Version(s)
I reproduced this in 1.2.25 and 1.3.1. I suspect it's been around for awhile, so maybe I'm just doing something wrong...?
Browsers and Operating System
This problem appears in Chrome & IE9, and affects HTML5 mode and hashbangs.
Reproduce the Error
This Plunkr shows this in practice. Click the
$location.url('/path')
button to see$location.url()
update, and then click on$location.url('')
to see that$location.url()
is unaffected.Plunkr: http://plnkr.co/edit/N6oPdq1RW5kMbvJ9IbfK?p=preview
Related Issues
I searched through the issue database and couldn't find anything related to this, so maybe I'm doing something wrong...
Suggest a Fix
The problem appears to be right here:
When
PATH_MATCH
runs against "", it matches correctly, somatch[1]
is''
. However, an empty string is falsy, sothis.path(...)
is never called, and nothing happens.Looks like a fairly simple fix, I can likely put a PR together this afternoon if I'm not off base here...
The text was updated successfully, but these errors were encountered: