AngularJS 元素 onresize 事件 - 自定义指令 [英] AngularJS element onresize event - Custom Directive
本文介绍了AngularJS 元素 onresize 事件 - 自定义指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
AngularJS 是否与此等效:
elementObject.addEventListener("resize", myFunction);
我考虑过手表,但我认为这不是一个好的解决方案.
解决方案
创建自定义指令:
app.directive("myResize", function($parse) {返回 {链接:postLink};函数 postLink(scope, elem, attrs) {elem.on("resize", function (e) {var rs = $parse(attrs.myResize);rs(scope, {$event: e});范围.$应用();});}});
用法:
欲了解更多信息,
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,
- AngularJS Developer Guide - Creating Custom Directives
- AngularJS Comprehensive Directive API Reference
- AngularJS $parse Service API Reference
- AngularJS jqLite API Reference
这篇关于AngularJS 元素 onresize 事件 - 自定义指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文