Skip to content

Commit 2452e3c

Browse files
committed
feat($anchorScroll): Add support for explicit hash
Add support for `hash` being explicitly passed as parameter to `$anchorScroll` and fall back to `$location.hash()` otherwise. Closes angular#4568
1 parent 874cac8 commit 2452e3c

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

src/ng/anchorScroll.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ function $AnchorScrollProvider() {
3838
* @requires $rootScope
3939
*
4040
* @description
41-
* When called, it checks the current value of {@link ng.$location#hash $location.hash()} and
42-
* scrolls to the related element, according to the rules specified in the
43-
* [Html5 spec](http://dev.w3.org/html5/spec/Overview.html#the-indicated-part-of-the-document).
41+
* When called, it scrolls to the element related to the specified `hash` or (if omitted) to the
42+
* current value of {@link ng.$location#hash $location.hash()}, according to the rules specified
43+
* in the
44+
* [HTML5 spec](http://dev.w3.org/html5/spec/Overview.html#the-indicated-part-of-the-document).
4445
*
4546
* It also watches the {@link ng.$location#hash $location.hash()} and automatically scrolls to
4647
* match any anchor whenever it changes. This can be disabled by calling
@@ -49,6 +50,9 @@ function $AnchorScrollProvider() {
4950
* Additionally, you can use its {@link ng.$anchorScroll#yOffset yOffset} property to specify a
5051
* vertical scroll-offset (either fixed or dynamic).
5152
*
53+
* @param {string} hash The hash specifying the element to scroll to. If omitted,
54+
* {@link ng.$location#hash $location.hash()} will be used.
55+
*
5256
* @property {(number|function|jqLite)} yOffset
5357
* If set, specifies a vertical scroll-offset. This is often useful when there are fixed
5458
* positioned elements at the top of the page, such as navbars, headers etc.
@@ -233,8 +237,9 @@ function $AnchorScrollProvider() {
233237
}
234238
}
235239

236-
function scroll() {
237-
var hash = $location.hash(), elm;
240+
function scroll(hash) {
241+
hash = (arguments.length && isString(hash)) ? hash : $location.hash();
242+
var elm;
238243

239244
// empty hash, scroll to the top of the page
240245
if (!hash) scrollTo(null);

test/ng/anchorScrollSpec.js

+37-33
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ describe('$anchorScroll', function() {
4949
};
5050
}
5151

52-
function callAnchorScroll() {
52+
function callAnchorScroll(hash) {
53+
var args = arguments;
5354
return function($anchorScroll) {
54-
$anchorScroll();
55+
$anchorScroll.apply(null, args);
5556
};
5657
}
5758

@@ -136,55 +137,58 @@ describe('$anchorScroll', function() {
136137
}));
137138

138139

139-
describe('when explicitly called', function() {
140+
describe('when implicitly using `$location.hash()`', function() {
140141

141142
beforeEach(createMockWindow());
142143

143144

144-
it('should scroll to top of the window if empty hash', inject(
145-
changeHashAndScroll(''),
146-
expectScrollingToTop));
145+
describe('and no hash is specified', function() {
146+
147+
it('should scroll to top of the window if empty hash', inject(
148+
changeHashAndScroll(''),
149+
expectScrollingToTop));
147150

148151

149-
it('should not scroll if hash does not match any element', inject(
150-
addElements('id=one', 'id=two'),
151-
changeHashAndScroll('non-existing'),
152-
expectNoScrolling()));
152+
it('should not scroll if hash does not match any element', inject(
153+
addElements('id=one', 'id=two'),
154+
changeHashAndScroll('non-existing'),
155+
expectNoScrolling()));
153156

154157

155-
it('should scroll to anchor element with name', inject(
156-
addElements('a name=abc'),
157-
changeHashAndScroll('abc'),
158-
expectScrollingTo('a name=abc')));
158+
it('should scroll to anchor element with name', inject(
159+
addElements('a name=abc'),
160+
changeHashAndScroll('abc'),
161+
expectScrollingTo('a name=abc')));
159162

160163

161-
it('should not scroll to other than anchor element with name', inject(
162-
addElements('input name=xxl', 'select name=xxl', 'form name=xxl'),
163-
changeHashAndScroll('xxl'),
164-
expectNoScrolling()));
164+
it('should not scroll to other than anchor element with name', inject(
165+
addElements('input name=xxl', 'select name=xxl', 'form name=xxl'),
166+
changeHashAndScroll('xxl'),
167+
expectNoScrolling()));
165168

166169

167-
it('should scroll to anchor even if other element with given name exist', inject(
168-
addElements('input name=some', 'a name=some'),
169-
changeHashAndScroll('some'),
170-
expectScrollingTo('a name=some')));
170+
it('should scroll to anchor even if other element with given name exist', inject(
171+
addElements('input name=some', 'a name=some'),
172+
changeHashAndScroll('some'),
173+
expectScrollingTo('a name=some')));
171174

172175

173-
it('should scroll to element with id with precedence over name', inject(
174-
addElements('name=abc', 'id=abc'),
175-
changeHashAndScroll('abc'),
176-
expectScrollingTo('id=abc')));
176+
it('should scroll to element with id with precedence over name', inject(
177+
addElements('name=abc', 'id=abc'),
178+
changeHashAndScroll('abc'),
179+
expectScrollingTo('id=abc')));
177180

178181

179-
it('should scroll to top if hash == "top" and no matching element', inject(
180-
changeHashAndScroll('top'),
181-
expectScrollingToTop));
182+
it('should scroll to top if hash == "top" and no matching element', inject(
183+
changeHashAndScroll('top'),
184+
expectScrollingToTop));
182185

183186

184-
it('should scroll to element with id "top" if present', inject(
185-
addElements('id=top'),
186-
changeHashAndScroll('top'),
187-
expectScrollingTo('id=top')));
187+
it('should scroll to element with id "top" if present', inject(
188+
addElements('id=top'),
189+
changeHashAndScroll('top'),
190+
expectScrollingTo('id=top')));
191+
});
188192
});
189193

190194

0 commit comments

Comments
 (0)