AngularJS element onresize event - Custom Directive

血红的双手。 提交于 2020-01-05 05:28:06

问题


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.


回答1:


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,

  • AngularJS Developer Guide - Creating Custom Directives
  • AngularJS Comprehensive Directive API Reference
  • AngularJS $parse Service API Reference
  • AngularJS jqLite API Reference



回答2:


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


来源:https://stackoverflow.com/questions/45039891/angularjs-element-onresize-event-custom-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!