AngularJS元素onresize事件-自定义指令 [英] AngularJS element onresize event - Custom Directive

查看:311
本文介绍了AngularJS元素onresize事件-自定义指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS是否具有与此等效的功能?

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.

推荐答案

创建自定义指令:

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>

有关更多信息,

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

这篇关于AngularJS元素onresize事件-自定义指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆