Scrolling to specific element on different template after clicking link (Angularjs)

前端 未结 1 1471
耶瑟儿~
耶瑟儿~ 2021-01-23 12:16

Hey guys I am pretty new to Angular.

Users can edit profile settings in profileSettings page.

I have the following on my profile template:



        
相关标签:
1条回答
  • 2021-01-23 12:54

    Use the optional onEnter callback which will be called when profileSettings state becomes active to render those two functions :

    • $location.hash('hashValue') will take to the element with id="hashValue" url like : /profileSettings.html#hashValue
    • $anchorScroll will scrolls to that element

    So your code may look like this:

    $stateProvider.state('profileSettings', {
      url: '/settings',
      template: 'settings.html',
      controller: 'ProfileSettingsCtrl',
      onEnter: function(){
          $location.hash('hashValue');
          $anchorScroll();
      }
    });
    

    Note: Remember to add $location and $anchorScrollto your dependencies.

    0 讨论(0)
提交回复
热议问题