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

fix($anchorScroll): don't scroll to top when initializing and location h... #9393

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/ng/anchorScroll.js
Original file line number Diff line number Diff line change
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;

$rootScope.$evalAsync(scroll);
});
}
Expand Down
17 changes: 17 additions & 0 deletions test/ng/anchorScrollSpec.js
Original file line number Diff line number Diff line change
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