-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Proposal: Merge the $anchorScroll service into $location #5532
Comments
This sounds quite good. Thanks for the thorough explanation. I scheduled this for 1.3.x and I'd love for you to work on it BUT now is not the time. There are still bugs we want to fix in 1.2 before forking. Making a PR now would likely result in a sad rebase later when we work on 1.3 Can you send us a PR when we announce that we branched 1.2.x and are starting to work on 1.3? Thanks a lot! |
@IgorMinar got it, thanks for explaining! |
+1 for $anchorProvider. Potentially later there could be a more than just settings in the provider, there could be a event registering for ex. |
+1 |
+1 Having had a hard time understanding how the Worried, tho, about how all this would affect the way |
@gsklee are you still interested in implementing this? Sorry it has taken a while to get back to it. We are about to start 1.5 so now would be a perfect time for this change. |
It seems that there is no interest in implementing this refactoring. |
This service only offers two things as it currently stands:
Make the browser be aware of a URL hash change and jump to that anchor; it's like the
$scope.$apply()
for the URL hash.Though unlike
$scope.$apply()
, which exists so we can work with any arbitrary JavaScript code, I don't see the reasoning in this case: if an element which explicitly sayshref="#anchor"
or$location.hash('anchor')
is clicked, and there exists exactly such an anchor namedid="anchor"
within the document, I fail to see any scenario where this _is not_ implying for an internal link, because this is the way hyperlink has been working since eons.Provide a watch mechanism and setting which is very confusing in its current implementation.
If I leave the default setting as is, the watch mechanism can be enabled by simply DI'ing
$anchorScroll
into any place in the code:Or, if you happen to have used
ngInclude
within your template, you don't even need to inject it becausengInclude
has done that for you:Of course, you won't wanna write your code like above with such obscurity, so chances are you'll be authoring your code like this (which I assume is the recommended usage because this is how the doc has it):
Now if you actually go check it you'll find that you're performing the click two times, one by you and the other the watch mechanism.
So if you wanna get everything right (better coding style, only click once), you gotta have it this way:
Btw, the name of the provider setting doesn't really match the API naming convention as well - think
$locationProvider.html5Mode(true)
.For reasons above I'm proposing a breaking change which will fulfill the following:
href="#anchor"
or$location.hash('anchor')
attached to it, is clicked, and there exists exactly such an anchor name within the document,$location
will treat that as an internal page link and jump to that section. Correct implementation will address $location should not intercept #links (in html4 mode) #772.$locationProvider.anchorScroll()
, which can be set as'normal'
or'smooth'
(because I've always been thinking this is what$anchorScroll
does, very disappointed to find out the truth yesterday 🙀). The'smooth'
part can be implemented later.$locationProvider.anchorScrollOffset()
, which addresses $anchorScroll Offset #2070. This can also be implemented later.$anchorProvider.scroll()
and$anchorProvider.scrollOffset()
. Like this one better but then we're left with a service that does nothing.Would like to look for some input before I send my PR, thanks!
The text was updated successfully, but these errors were encountered: