簡體   English   中英

基於另一個元素的高度更改元素樣式的angularjs方法是什么

[英]What is the angularjs way of changing an elements style based on the height of another element

$('#reply').css("padding-top", $('.post:visible').height()-35 + "px" );

我正在做的是將#reply的CSS padding-top#reply.post的高度。

到目前為止,我可以在我的AngularJS項目中使用此方法的唯一方法是:

$(function() {
  setTimeout(function () {
    $('#reply').css("padding-top", $('.post:visible').height()-35 + "px" )
  },300)
});

我將如何以Angular的方式進行操作,例如,類似的代碼是某種angular.copy 或者我可以繼續使用jQuery。 如果是這樣,當AngularJS完成后如何觸發它,所以我不需要超時功能?

<div id="#reply" ng-style="{'padding-top' : code_that_returns_.post:visible's_height }">

這個問題問得很少,但我決定我將逐步解決問題和回答。

目前,這是我的解決方案(它解決了我的超時問題)。

<div id="#reply" ng-style="{'padding-top' : calcPostHeight() }">

$scope.calcPostHeight = function () {
  return $('.post:visible').height()-35 + "px";
};

編輯:

該解決方案似乎更糟...但是使用了指令。

<div id="#reply" post-height="35">

.directive('postHeight', function($timeout){
  return {
    link: {
      post: function (scope, element, attr) {
        $timeout(function(){
          var result = document.getElementsByClassName("post")[0];
          element[0].style.paddingTop = result.style.width - attr.postHeight;
        }, 0)
      }
    }
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM