Skip to content

Commit ee26228

Browse files
committed
feat(uiView): cache and test autoscroll expression
Changes to follow ngInclude's behavior more closely (like #714): - memorizes the autoscroll expression, since it's not supposed to be changed once the template is compiled, only the evaluated expression should change; - the page won't autoscroll when the attribute isn't present, which is a breaking change from the current implementation.
1 parent 8bb9e27 commit ee26228

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/viewDirective.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
3131
var viewScope, viewLocals,
3232
name = attr[directive.name] || attr.name || '',
3333
onloadExp = attr.onload || '',
34+
autoscrollExp = attr.autoscroll,
3435
animate = $animator && $animator(scope, attr),
3536
initialView = transclude(scope);
3637

@@ -123,7 +124,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
123124

124125
// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
125126
// $anchorScroll might listen on event...
126-
if (!angular.isDefined(attr.autoscroll) || (!attr.autoscroll || scope.$eval(attr.autoscroll))) {
127+
if (angular.isDefined(autoscrollExp) && (!autoscrollExp || scope.$eval(autoscrollExp))) {
127128
$anchorScroll();
128129
}
129130
}

test/viewDirectiveSpec.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ describe('uiView', function () {
216216
});
217217

218218
describe('autoscroll attribute', function () {
219-
it('should autoscroll when unspecified', inject(function ($state, $q, $anchorScroll) {
219+
it('should not autoscroll when unspecified', inject(function ($state, $q, $anchorScroll) {
220220
elem.append($compile('<div ui-view></div>')(scope));
221221
$state.transitionTo(gState);
222222
$q.flush();
223-
expect($anchorScroll).toHaveBeenCalled();
223+
expect($anchorScroll).not.toHaveBeenCalled();
224224
}));
225225

226226
it('should autoscroll when expr is missing', inject(function ($state, $q, $anchorScroll) {
@@ -230,15 +230,17 @@ describe('uiView', function () {
230230
expect($anchorScroll).toHaveBeenCalled();
231231
}));
232232

233-
it('should autoscroll when truthy', inject(function ($state, $q, $anchorScroll) {
234-
elem.append($compile('<div ui-view autoscroll="true"></div>')(scope));
233+
it('should autoscroll when expression is truthy', inject(function ($state, $q, $anchorScroll) {
234+
elem.append($compile('<div ui-view autoscroll="doAutoScroll"></div>')(scope));
235+
scope.doAutoScroll = true;
235236
$state.transitionTo(gState);
236237
$q.flush();
237238
expect($anchorScroll).toHaveBeenCalled();
238239
}));
239240

240-
it('should not autoscroll when falsy', inject(function ($state, $q, $anchorScroll) {
241-
elem.append($compile('<div ui-view autoscroll="false"></div>')(scope));
241+
it('should not autoscroll when expression is falsy', inject(function ($state, $q, $anchorScroll) {
242+
elem.append($compile('<div ui-view autoscroll="doAutoScroll()"></div>')(scope));
243+
scope.doAutoScroll = function () { return false; };
242244
$state.transitionTo(gState);
243245
$q.flush();
244246
expect($anchorScroll).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)