I am using AngularJS's locationhash() + $anchorScroll to move a selected page element to the top of the window once its contents have loaded in via Ajax.
JS: In the controller:
$scope.scrollTo = function (location) {
//Scroll to category head
$scope.categoryHead = "grouptitle-" + location;
$location.hash($scope.categoryHead);
$anchorScroll($scope.categoryHead);
};
In the directive:
.directive('onFinishRender', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attr) {
var scroll;
if (scope.$last === true) {
$timeout(function () {
//Scroll category to top of page after list has completed render
scroll = scope.scrollTo(scope.category);
});
}
}
};
This gives me a display URL of mysite.com/##grouptitle-2
or similar, which looks a bit arcane. Is there any way to configure this anchor so that it either displays with only one hash, or doesn't modify the address bar URL at all?
on your $anchorScroll
remove the parameter. The anchorScroll will automatically grab the hash from $location.hash(<hash>)
.
so your function should look something like this:
$scope.categoryHead = "grouptitle-" + location;
$location.hash($scope.categoryHead);
$anchorScroll();
If the DOM is updated dynamically then wrap the $location.hash
and $anchorScroll
inside $timeout
function.
Do following steps: after this $location.hash($scope.categoryHead); add
$anchorScroll(); $location.hash(''); $location.replace();
$location.replace() function will remove '#' from url.
Do following steps: after this $location.hash($scope.categoryHead); add
$anchorScroll();
$location.hash('');
$location.replace();
$location.replace() function will remove '#' from url.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.