angularjs - Scroll to anchor after page load in Angular -
i need set ng-click event that loads new page, , then, once page has loaded, scrolls anchor point on page. i've tried every solution proposed on this post can't work correctly.
most of solutions center around scrolling anchor on page that's loaded. need scroll occur after new page has loaded.
here view code:
<div class="see-jobs-btn" ng-click="$event.stoppropagation();gotoresultjobs(r)">see jobs</div>
this represents button inside 'profile card'. when user clicks on card, takes them profile page. however, when click button, needs take them #jobs portion of profile page (hence $stoppropogation() before gotoresultjobs(r) in code).
here gotoresultjobs method looks like.
$scope.gotoresultjobs = function(result) { var profileurl = result.slug; window.location = profileurl; };
i've tried using $anchorscroll
, hardcoding in anchor profileurl
, neither 1 works. i'm new angular, don't know i'm missing here.
update 1: trying use $timeout
here gotoresultjobs
method within resultsctrl triggered when user clicks button:
$scope.gotoresultjobs = function(result) { var url = window.location + result.slug + '#jobs'; location.replace(url); };
that loads /name#jobs
path, calls profilectrl below:
app.controller('profilectrl', ['$scope', '$http', '$timeout', '$location', '$anchorscroll', function($scope, $http, $timeout, $location, $anchorscroll) { if(window.location.hash) { $timeout(function() { console.log('test'); // $location.hash('jobs'); // $location.hash('jobs'); $anchorscroll(); }, 1000); }; }]);
this setup seems work, test
appears in console when jobs button clicked, not when user clicks on profile. problem i'm facing page starts loading, , path in url bar changes /name#jobs
, before page finishes loading, jobs
stripped url. therefore, when $anchorscroll()
called, there no anchor tag in hash scroll to.
so pointed out, $anchorscroll
has occur after page has been rendered, otherwise anchor doesn't exist. can achieved using $timeout()
.
$timeout(function() { $anchorscroll('myanchor'); });
you can see this plunkr. make sure view in pop-out mode (the little blue button in upper right corner of output screen).
Comments
Post a Comment