diff --git a/src/viewDirective.js b/src/viewDirective.js index efc893070..2c4f3d0b9 100644 --- a/src/viewDirective.js +++ b/src/viewDirective.js @@ -14,6 +14,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an var viewScope, viewLocals, name = attr[directive.name] || attr.name || '', onloadExp = attr.onload || '', + autoScrollExp = attr.autoscroll, animate = $animator && $animator(scope, attr), initialView = transclude(scope); @@ -104,9 +105,10 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an viewScope.$emit('$viewContentLoaded'); if (onloadExp) viewScope.$eval(onloadExp); - // TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary? - // $anchorScroll might listen on event... - $anchorScroll(); + if (angular.isDefined(autoScrollExp) + && (!autoScrollExp || scope.$eval(autoScrollExp))) { + $anchorScroll(); + } } }; } diff --git a/test/viewDirectiveSpec.js b/test/viewDirectiveSpec.js index 2b182bc7e..765ea7589 100644 --- a/test/viewDirectiveSpec.js +++ b/test/viewDirectiveSpec.js @@ -69,7 +69,12 @@ describe('uiView', function () { template: 'jState' }; - beforeEach(module(function ($stateProvider) { + var count; + var $anchorScroll = function() { + count++; + }; + + beforeEach(module(function ($stateProvider, $provide) { $stateProvider .state('a', aState) .state('b', bState) @@ -81,6 +86,8 @@ describe('uiView', function () { .state('g.h', hState) .state('i', iState) .state('j', jState); + + $provide.factory('$anchorScroll', function(){ return $anchorScroll; }); })); beforeEach(inject(function ($rootScope, _$compile_) { @@ -209,4 +216,69 @@ describe('uiView', function () { })); }); + describe('autoscroll', function() { + beforeEach(function() { + count = 0; + }); + + it('should call anchor scroll if autoscroll is specified without a value', inject(function ($state, $q, $anchorScroll) { + elem.append($compile('
')(scope)); + + $state.transitionTo(aState); + $q.flush(); + + expect(count).toBe(1); + })); + + it('should call anchor scroll if autoscroll is true', inject(function ($state, $q, $anchorScroll) { + elem.append($compile('')(scope)); + + $state.transitionTo(aState); + $q.flush(); + + expect(count).toBe(1); + })); + + it('should not call anchor scroll if autoscroll is false', inject(function ($state, $q, $anchorScroll) { + elem.append($compile('')(scope)); + + $state.transitionTo(aState); + $q.flush(); + + expect(count).toBe(0); + })); + + it('should correctly evaluate the expression', inject(function ($state, $q, $anchorScroll) { + scope.thing = true; + scope.$apply(); + elem.append($compile('')(scope)); + + $state.transitionTo(aState); + $q.flush(); + + scope.thing = true; + scope.$apply(); + + expect(count).toBe(1); + count = 0; + + scope.thing = false; + scope.$apply(); + + $state.transitionTo(bState); + $q.flush(); + + expect(count).toBe(0); + })); + + it('should not call anchor scroll if autoscroll is not set', inject(function ($state, $q, $anchorScroll) { + elem.append($compile('')(scope)); + + $state.transitionTo(aState); + $q.flush(); + + expect(count).toBe(0); + })); + }); + });