使用AngularJS从某些服务加载内容后,需要获取html文档(正文)的高度。

 /* SHOW RECENT POSTS */
  app.controller('RecentPostsController', function ($scope, $http) {

    $http.get("/site/recentpostsjson/" + appname).success(function (data) {
        $scope.posts = data;

        /* HEIGHT FROM THIS IS COMING DEFAULT , NOT WHAT WE GET AFTER DATA
           LOAD FROM ANGULAR JS */
        var contentHeight = window.document.body.scrollHeight;
        alert(contentHeight);
    });
});

最佳答案

也许这有帮助

app.controller('RecentPostsController', function ($rootScope, $scope, $http) {

   $http.get("/site/recentpostsjson/" + appname).success(function (data) {
       $scope.posts = data;
       $rootScope.$broadcast('contentLoaded')
   });

});

app.directive('contentHeight', ['$timeout', '$rootScope', function($timeout, $rootScope){
    return {
        link: function(scope, elem, attrs){
              $rootScope.$on('contentLoaded', function(){
                  $timeout(function(){elem[0].scrollHeight});
              })
        }
    }
})
<body ng-app="someApp" content-height><!--All you staff --></body>


您只能在指令中操作DOM!
附言超时服务仅运行回调函数,然后摘要停止

没有超时http://jsfiddle.net/zcbzajqL/

关于javascript - 从AngularJS完成数据加载后获取HTML正文高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25644543/

10-12 02:59