Skip to content

Commit 5fd1efc

Browse files
committed
feat(uiView) autoscroll attribute
Adds an `autoscroll="expr"` attribute to `uiView` just like `ngInclude` has, with the difference that if the `autoscroll` attribute isn't defined then the scroll will happen in order to not break the current behavior.
1 parent 8ee2afb commit 5fd1efc

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/viewDirective.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
123123

124124
// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
125125
// $anchorScroll might listen on event...
126-
$anchorScroll();
126+
if (!angular.isDefined(attr.autoscroll) || scope.$eval(attr.autoscroll)) {
127+
$anchorScroll();
128+
}
127129
}
128130
};
129131
}

test/viewDirectiveSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ describe('uiView', function () {
1717

1818
beforeEach(module('ui.router'));
1919

20+
beforeEach(module(function ($provide) {
21+
$provide.decorator('$anchorScroll', function ($delegate) {
22+
return jasmine.createSpy('$anchorScroll');
23+
});
24+
}));
25+
2026
var aState = {
2127
template: 'aState template'
2228
},
@@ -209,4 +215,27 @@ describe('uiView', function () {
209215
}));
210216
});
211217

218+
describe('autoscroll attribute', function () {
219+
it('should autoscroll when missing', inject(function ($state, $q, $anchorScroll) {
220+
elem.append($compile('<div ui-view></div>')(scope));
221+
$state.transitionTo(gState);
222+
$q.flush();
223+
expect($anchorScroll).toHaveBeenCalled();
224+
}));
225+
226+
it('should autoscroll when truthy', inject(function ($state, $q, $anchorScroll) {
227+
elem.append($compile('<div ui-view autoscroll="true"></div>')(scope));
228+
$state.transitionTo(gState);
229+
$q.flush();
230+
expect($anchorScroll).toHaveBeenCalled();
231+
}));
232+
233+
it('should not autoscroll when falsy', inject(function ($state, $q, $anchorScroll) {
234+
elem.append($compile('<div ui-view autoscroll="false"></div>')(scope));
235+
$state.transitionTo(gState);
236+
$q.flush();
237+
expect($anchorScroll).not.toHaveBeenCalled();
238+
}));
239+
});
240+
212241
});

0 commit comments

Comments
 (0)