javascript - angularjs的controller里面获取不到图片的高度怎么办?
伊谢尔伦
伊谢尔伦 2017-04-11 11:59:40
0
1
234

我想打算用 offsetHeight 获取图片的高度 。
angular.element(img).ready 这个是dom树加载完就立即执行了。此时图片还没显示出来;所以offsetHeight 获取的高度是0.

想用onload 但是在controller 里面好像是无效的。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

répondre à tous(1)
洪涛

要引入angular版的jquery 叫jqLite

angular.element('#img').height();
或者
angular.element(document.querySelector('#img'))[0].offsetHeight

<body ng-app="myModule">
    <h1 my-directive>Hello Plunker! width: {{width}}, height: {{height}}
    </h1>
  </body>


angular.module('myModule', [])
  .directive('myDirective', function($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element) {
            scope.height = element.prop('offsetHeight');
            scope.width = element.prop('offsetWidth');
        }
    };
  })
;
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!