简体   繁体   中英

AngularJS element onresize event - Custom Directive

Does AngularJS have equivalent to this:

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:

<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
            })
        }
    }
}])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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