[英]How to capture resize event of any DOM element in angular
I have two components.我有两个组件。
1st components contain a form
with textara
control, and the 2nd component contains an SVG
graph.第一个组件包含一个带有textara
控件的form
,第二个组件包含一个SVG
图。 When user adds more detailed in textarea
control and due to that form element get resized, however, I want to detect it to make my SVG
graph aligned to that change.但是,当用户在textarea
控件中添加更多详细信息并且由于该表单元素的大小被调整时,我想检测它以使我的SVG
图形与该更改对齐。
<div id="comp1">
<form action="/action_page.php" id="usrform">
<textarea rows="4" cols="50" name="comment" form="usrform">
Enter text here...</textarea>
</form>
<div>
<div id="comp2">
</div>
I want to detect changes whenever comp1
get resized.我想在comp1
调整大小时检测更改。
You can do that with the (window:resize)
function.您可以使用(window:resize)
function 来做到这一点。
<div id="comp1" (window:resize)="yourFunction($event)"> // <-- add this code fragment
<form action="/action_page.php" id="usrform">
<textarea rows="4" cols="50" name="comment" form="usrform">
Enter text here...</textarea>
</form>
<div>
<div id="comp2">
</div>
Angular provides HostListener
Decorator that declares a DOM event to listen for, and provides a handler method to run when that window resize event occurs. Angular 提供了HostListener
装饰器,它声明了要监听的 DOM 事件,并提供了在window 调整大小事件发生时运行的处理程序方法。
as below如下
import {HostListener} from '@angular/core';
class AppComponent{
@HostListener('window:resize', ['$event'])
resize(event) {
console.log(event);
}
}
If you want to trigger an resize event on div resize then you can create custom directive
as provided by https://www.npmjs.com/package/angular-resize-event package.如果要在 div resize 上触发 resize 事件,则可以创建https://www.npmjs.com/package/angular-resize-event package 提供的custom directive
。 You can use that package directly or copy the directive
source code from here and use it in your project.您可以直接使用 package 或从此处复制directive
源代码并在您的项目中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.