2

我试图在 Angular 1.x 中使用一个非常简单的 scrollTo 。我有导航菜单链接,单击该链接应滚动到 div#id。我只能通过“承诺”来实现这一点。

例如,这有效:

<li><a href="#" ng-click="tracking()">Go to Tracking</a></li>


$scope.tracking = function() { // go to tracking when header button is clicked
        $http.get('/includes/modules/get_home_destinations.php')
        .then(function(reply){
            if (reply.data) {
                $scope.destinations = reply.data;
                $location.hash('home-tracking');
            }         
        });
    };

但这没有回应:

$scope.tracking = function() { // go to tracking when header button is clicked
     $location.hash('home-tracking');
};

就好像需要一个承诺,但是要让它在没有承诺的情况下通过简单的点击工作?

4

2 回答 2

1

希望下面的代码有用:

在 html 文件中

<div id="home-tracking">
    <h1>Home Traking Content</h1>
</div>

<a ng-click="tracking()">Go to Tracking</a>

在控制器文件中

angular.module('trackingApp').controller('TrackingCtrl', function($location, $anchorScroll) {
    $scope.tracking = function() {
          $location.hash('home-tracking');
          $anchorScroll();
    }
})
于 2017-09-16T10:43:35.793 回答
1

我猜这是因为 href="#" 。因为首先,href 将页面重定向到“#”,然后承诺会延迟执行并将页面重定向到所需的位置。但是没有承诺,就没有时间延迟,代码立即执行,href 将页面重定向到'#'并且页面卡在那里。

于 2017-09-16T10:57:44.190 回答