$anchorScroll not working on after back to Index.h

2019-08-13 03:44发布

问题:

Hello I am new in angular JS and Onsen UI. i have problem $anchorScroll not working if only i back to page1 in index.html. everything is fine if i refresh browser. in index.html i have two page :

  1. page1. there is button to navigate to page2
  2. in page2 i have number list : from 1 to 20. After open page2 automatically scroll to list number 5 and the $anchorScroll is working fine.
     $scope.scrollTo = function(elementId) {
         $timeout(function() {
              $location.hash(elementId);
              $anchorScroll();
          });

      };

      init();
      function init(){

        $scope.scrollTo('no-5');

      }

in bottom on the list i have OK button that is back to page1 (index.html) the code is

       $scope.back = function(elementId) {
         $scope.myNavigator.pushPage('index.html');

      };
  1. The problem is after i back to page1 and then navigate to page2 again. this $anchorScroll is not working. why this is happen? i have written the code in codepen the link is below

http://codepen.io/lapisan/pen/LGbbJL

回答1:

If you use myNavigator.pushPage you are adding a new page to the stack. In your case, it's better if you reset the navigator to index.html by using myNavigator.resetToPage method.

$scope.back = function(elementId) {
  $scope.myNavigator.resetToPage('index.html');
};

Here is a fixed version of your CodePen example, hope it helps!

http://codepen.io/andipavllo/pen/LGbQgy