简体   繁体   English

Angular指令绑定到元素的高度

[英]Angular directive bind to height of element

I am fairly new to Angular and want to be able to bind to the height of an element. 我对Angular很新,并希望能够绑定到元素的高度。 In my current case I want to bind the CSS bottom on el1 to the height of el2 . 在我目前的情况下,我想将el1上的CSS bottom绑定到el2的高度。 They do not share a common controller. 他们不共享一个共同的控制器。 How can I do this? 我怎样才能做到这一点?

<div id='el1' ng-controller='controller1' style='bottom: {{theHeightOfEl2}}'></div>
<div id='el2' ng-controller='controller2' style='height: 573px;'></div>

I found an answer on here which looked promising but couldnt see how to extend it to allow me to specify which element I wanted to bind it to. 我在这里找到了一个看起来很有希望的答案,但无法看到如何扩展它以允许我指定我想要绑定它的元素。

In a generic case, I guess what I am asking for is a directive to bind property X on Element 1 to property Y on Element 2. 在一般情况下,我想我要求的是将元素1上的属性X绑定到元素2上的属性Y的指令。

Update 更新

I have created a directive to do this but it isn't quite working yet. 我已经创建了一个指令来执行此操作,但它尚未完成。 It fires correctly at the start but when I try to test it by manually updating the CSS of the el2 the watch doesnt fire 它在开始时正确触发,但是当我尝试通过手动更新el2的CSS来测试时,手表不会触发

m.directive('bindToHeight', function ($window) {
    return {
        restrict: 'A',
        link: function (scope, elem, attrs) {
            var attributes = scope.$eval(attrs['bindToHeight']);
            var targetElem = angular.element(document.querySelector(attributes[1]));

            // Watch for changes
            scope.$watch(function () {
                return targetElem.height();
            },
            function (newValue, oldValue) {
                if (newValue != oldValue) {
                    // Do something ...
                    console.log('Setting bottom to ' + newValue);
                    elem.attr('bottom', newValue);
                }
            });
        }
    };
});

To anyone looking for the answer, here is what I used 对于任何寻找答案的人来说,这就是我所使用的

Usage bind-to-height="[cssProperty, sourceElement]" : 用法bind-to-height="[cssProperty, sourceElement]"

<div bind-to-height="['bottom','#addressBookQuickLinks']">

Code: 码:

m.directive('bindToHeight', function ($window) {

    return {
        restrict: 'A',

        link: function (scope, elem, attrs) {
            var attributes = scope.$eval(attrs['bindToHeight']);
            var targetElem = angular.element(document.querySelector(attributes[1]));

            // Watch for changes
            scope.$watch(function () {
                return targetElem.height();
            },
            function (newValue, oldValue) {
                if (newValue != oldValue) {
                    elem.css(attributes[0], newValue);
                }
            });
        }
    };
});

you can wrap you controllers in additional top level controller for example 例如,您可以将控制器包装在额外的顶级控制器中

<div ng-controller="PageController">
 <div id='el1' ng-controller='controller1' style='bottom: {{theHeightOfEl2}}'></div>
 <div id='el2' ng-controller='controller2' style='height: 573px;'></div>
</div>

and calculate and store the height of element in that controller 并计算并存储该控制器中元素的高度

another way to create directive and apply on one of them, which will find the element from DOM and will apply the height on that 另一种创建指令并在其中一个上应用的方法,它将从DOM中找到元素,并将在其上应用高度

You can easily do it with this simple directive. 您可以使用这个简单的指令轻松完成。 Basically it only watches for that attribute value to change. 基本上它只会监视要更改的属性值。

app.directive('bindHeight', function ($window) {
  return {
    link:  function(scope, element, attrs){
      scope.$watch(attrs.bindHeight, function(value) {
        console.log(value);
        element.css('height',value + 'px');
      });
    }
  };
});


//$scope.myHeight
<div id="main-canvas" bind-height="myHeight">

I'm using a modified version of what you did. 我正在使用您所做的修改版本。 I added an offset attribute and a timeout for letting ui rendering events to complete: 我添加了一个偏移属性和一个超时,让ui渲染事件完成:

m.directive('bindToHeight', function ($window) {
    return {
        restrict: 'A',
        link: function(scope, iElement, iAttrs) {
            var attributes = scope.$eval(iAttrs.bindToHeight);
            var targetElem = angular.element(document.querySelector(attributes[1]));
            var offset = attributes[2]? parseInt(attributes[2]) : 0;
            var timeoutId;
            // Watch for changes
            scope.$watch(function () {
                timeoutId && $timeout.cancel(timeoutId);
                timeoutId = $timeout(function () {
                  var total = targetElem.height() + offset;
                  //remove the px/em etc. from the end before comparing..
                  if (parseInt(iElement.css(attributes[0]).slice(0, -2)) !== total)
                  {
                    iElement.css(attributes[0], total);
                  }
                }, 50, false);
            });
        }
    };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM