简体   繁体   English

AngularJS元素onresize事件-自定义指令

[英]AngularJS element onresize event - Custom Directive

Does AngularJS have equivalent to this: AngularJS是否具有与此等效的功能:

elementObject.addEventListener("resize", myFunction);

I thought about about watches, but I don't think it's the good solution. 我考虑过手表,但我认为这不是一个好的解决方案。

Create a custom directive: 创建一个自定义指令:

app.directive("myResize", function($parse) {
    return {
        link: postLink
    };
    function postLink(scope, elem, attrs) {
        elem.on("resize", function (e) {
            var rs = $parse(attrs.myResize);
            rs(scope, {$event: e});
            scope.$apply();
        });
    }
});

Usage: 用法:

<div my-resize="$ctrl.myFunction($event)">
</div>

For more information, 欲获得更多信息,

You can also do this: HTML: 您还可以执行以下操作:HTML:

<div resize-me></div>

JavaScript: JavaScript的:

myApp.directive('resizeMe', ['$window', function($window) {
    return {
        link: function(scope, elem, attrs) {
            angular.element(elem).bind('resize', function() {
                //do something on resize
            })
        }
    }
}])

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

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