Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($anchorScroll): don't scroll to top when initializing and locatio…
Browse files Browse the repository at this point in the history
…n hash is empty

Closes #8848
Closes #9393
  • Loading branch information
IgorMinar committed Oct 8, 2014
1 parent df1a00b commit d5445c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ng/anchorScroll.js
Expand Up @@ -94,7 +94,10 @@ function $AnchorScrollProvider() {
// (no url change, no $location.hash() change), browser native does scroll
if (autoScrollingEnabled) {
$rootScope.$watch(function autoScrollWatch() {return $location.hash();},
function autoScrollWatchAction() {
function autoScrollWatchAction(newVal, oldVal) {
// skip the initial scroll if $location.hash is empty
if (newVal === oldVal && newVal === '') return;

This comment has been minimized.

Copy link
@gkalpak

gkalpak Oct 8, 2014

Member

@IgorMinar : Can this actually ever happen ? To have a watchAction triggered even if newVal === oldVal and without being undefined (as happens the first time a watchAction is called for initialization) ?
Should this rather be if (newVal === oldVal) return; (or am I missing something) ?

This comment has been minimized.

Copy link
@IgorMinar

IgorMinar via email Oct 9, 2014

Author Contributor

$rootScope.$evalAsync(scroll);
});
}
Expand Down
17 changes: 17 additions & 0 deletions test/ng/anchorScrollSpec.js
Expand Up @@ -106,6 +106,12 @@ describe('$anchorScroll', function() {

describe('watcher', function() {

function initAnchorScroll() {
return function($rootScope, $anchorScroll) {
$rootScope.$digest();
};
}

function initLocation(config) {
return function($provide, $locationProvider) {
$provide.value('$sniffer', {history: config.historyApi});
Expand Down Expand Up @@ -135,6 +141,7 @@ describe('$anchorScroll', function() {
it('should scroll to element when hash change in hashbang mode', function() {
module(initLocation({html5Mode: false, historyApi: true}));
inject(
initAnchorScroll(),
addElements('id=some'),
changeHashTo('some'),
expectScrollingTo('id=some')
Expand All @@ -145,16 +152,25 @@ describe('$anchorScroll', function() {
it('should scroll to element when hash change in html5 mode with no history api', function() {
module(initLocation({html5Mode: true, historyApi: false}));
inject(
initAnchorScroll(),
addElements('id=some'),
changeHashTo('some'),
expectScrollingTo('id=some')
);
});


it('should not scroll to the top if $anchorScroll is initializing and location hash is empty',
inject(
initAnchorScroll(),
expectNoScrolling())
);


it('should not scroll when element does not exist', function() {
module(initLocation({html5Mode: false, historyApi: false}));
inject(
initAnchorScroll(),
addElements('id=some'),
changeHashTo('other'),
expectNoScrolling()
Expand All @@ -165,6 +181,7 @@ describe('$anchorScroll', function() {
it('should scroll when html5 mode with history api', function() {
module(initLocation({html5Mode: true, historyApi: true}));
inject(
initAnchorScroll(),
addElements('id=some'),
changeHashTo('some'),
expectScrollingTo('id=some')
Expand Down

0 comments on commit d5445c6

Please sign in to comment.