简体   繁体   English

AngularJS指令监视父级大小的变化

[英]AngularJS directive watch parent size change

Problem 问题

I have a simple directive that executes sizing updates on a particular element. 我有一个简单的指令,执行特定元素的大小更新。 This watches the window size and makes adjustments accordingly. 这会监视窗口大小并相应地进行调整。

MyApp.directive('resizeTest', ['$window', function($window) {
  return {
    restrict: 'AC',
    link: function(scope, element) {
      var w = angular.element($window);
      scope.$watch(function() {
        return { 'h': w.height(), 'w': w.width() };
      }, function(newValue, oldValue) {
        // resizing happens here
      }, true);
      w.bind('resize', function() { scope.$apply(); });
    }
  };
}]);

This works just fine. 这很好用。

It just so happens that inside of the div tag that is associated with, I have a child div . 它恰好发生在与之关联的div标签内部,我有一个子div When the parent is resized, I want to make positioning alterations on the child element. 调整父级的大小时,我想对子元素进行定位更改。 However, I cannot get the trigger to fire. 但是,我无法触发。

This gets called at startup, but does not get triggered when the element is resized or the window changes: 这会在启动时调用,但在调整元素大小或窗口更改时不会触发:

MyApp.directive('centerVertical', ['$window', function($window) {
  return {
    restrict: 'AC',
    link: function(scope, element) {
      element.css({border: '1px solid #0000FF'});
      scope.$watch('data.overlaytype', function() {
        $window.setTimeout(function() {
          console.log('I am:      ' + element.width() + 'x' + element.height());
          console.log('Parent is: ' + element.parent().width() + 'x' + element.parent().height());
        }, 1);
      });
    }
  };
}]);

What type of binding or watch configuration should I be using to check for resizing of the parent element? 我应该使用什么类型的绑定或监视配置来检查父元素的大小调整?

Fiddle 小提琴

https://jsfiddle.net/rcy63v7t/1/ https://jsfiddle.net/rcy63v7t/1/

The value data.overlaytype you are observing in the centerVertical directive is not on the scope so the resulting value is undefined and this value never changes that's why you dont get the listener executed. 您在centerVertical指令中观察到的值data.overlaytype不在scope上,因此结果值是undefined并且此值永远不会改变,这就是您不执行侦听器的原因。 To check if the size of the parent element changed you can check it in the $watch function like this: 要检查父元素的大小是否已更改,您可以在$ watch函数中检查它,如下所示:

scope.$watch(
    function () { 
        return {
           width: element.parent().width(),
           height: element.parent().height(),
        }
   },
   function () {}, //listener 
   true //deep watch
);

Moreover remember, when you want to use an already existing module you can't call it like this angular.module('myModule', []) because this mean creating new module. 此外,请记住,当你想使用一个已经存在的模块时,你不能像这个angular.module('myModule', [])那样调用它angular.module('myModule', [])因为这意味着创建新模块。 You have to just pass the module name angular.module('myModule') , which is second thing why your code didn't work. 你必须传递模块名称angular.module('myModule') ,这是你的代码无法工作的第二个原因。

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

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