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

AngularJS element onresize event - Custom Directive

AngularJS是否有等同于此的:

elementObject.addEventListener("resize", myFunction);

我考虑过手表,但我认为这不是好的解决方案。

创建自定义指令:

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();
        });
    }
});

用法:

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

欲了解更多信息,

您也可以这样做: HTML:

<div resize-me></div>

JavaScript:

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