Skip to content

Instantly share code, notes, and snippets.

@oaleynik
Last active December 15, 2015 17:59
Show Gist options
  • Save oaleynik/5300231 to your computer and use it in GitHub Desktop.
Save oaleynik/5300231 to your computer and use it in GitHub Desktop.
Problem - defer function invokation till the full browser render and till the end of view-model synchronization cycle
var Ctrl = function ($scope, initAnchorScrolling) {
initAnchorScrolling($scope);
};
Ctrl.$inject = ['$scope', "initAnchorScrolling"];
applicationServices.factory("initAnchorScrolling",
['scroll', '$location', '$timeout', function(scrollUtils, $location, $timeout) {
return function(scope) {
/** Some stuff here */
function scroll() {
var hash = $location.hash(), elm;
if (!hash) {
scrollUtils.scrollTo(0, 0);
} else if ((elm = getAnchorElement(hash))) {
scrollUtils.scrollToElement(elm);
} else {
scrollUtils.scrollTo(0, 0);
}
}
scope.$watch(function scrollWatch() { return $location.hash(); },
function scrollWatchAction() {
$timeout(function () {
scroll();
}, 0, true);
});
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment