angular - 如何在调整角度后修复元素
问题描述
我在 angular9 上使用“ResizableModule”,我的 div 现在可以调整大小,但是当我释放 div 时,它启动并返回到它的大小。
我用过:https ://www.npmjs.com/package/angular-resize-element那个包。
那么如何在发布后修复它的大小。
我的组件.ts:
import {MatTableDataSource} from '@angular/material/table';
export class SampleComponent implements OnInit
{
public style: object = {};
onResizeEnd(event: ResizeEvent): void {
console.log('Element was resized', event);
this.style = {
position: 'fixed',
left: `${event.rectangle.left}px`,
top: `${event.rectangle.top}px`,
width: `${event.rectangle.width}px`,
height: `${event.rectangle.height}px`
};
}
html:
<div
mwlResizable
[enableGhostResize]="true"
[resizeEdges]="{ bottom: true, right: true, top: true, left: true }"
(resizeEnd)="onResizeEnd($event)"
style="width: 95%;"
> </div>
组件.scss:
mwlResizable {
box-sizing: border-box;
padding-top: 10px;
padding-left: 10px;
padding-bottom: 10px;
padding-right: 10px;
}
stackBlitz 上有一个简单的例子:https ://stackblitz.com/edit/angular-3rowwg
解决方案
在调试你的 stackblitz 解决方案后,我注意到在 resizeEnd 函数末尾创建的样式 css 不适用于可调整大小的 div。
在这里,我使用 ngstyle 添加了样式,因此每当 this.style 变量更改时,新的 ngstyle 将根据变量调整 css
这是您的解决方案
app.component.js
import { Component } from '@angular/core';
import { ResizeEvent } from 'angular-resizable-element';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
public style: object = {};
onResizeEnd(event: ResizeEvent): void {
console.log('Element was resized', event);
this.leftPosition = event.rectangle.left;
this.topPosition = event.rectangle.top;
this.divWidth = event.rectangle.width;
this.divHeight = event.rectangle.height;
this.style = {
position: 'fixed',
left: `${this.leftPosition}px`,
top: `${this.topPosition}px`,
width: `${this.divWidth}px`,
height: `${this.divHeight}px`
};
console.log(this.style)
}
}
app.component.html
<div
mwlResizable
[enableGhostResize]="true"
[resizeEdges]="{ bottom: true, right: true, top: true, left: true }"
(resizeEnd)="onResizeEnd($event)"
style="border:1px solid red;" [ngStyle]="style"> i'm resizales</div>
这是我的解决方案的 stackblitz url:https ://stackblitz.com/edit/angular-efhdra
推荐阅读
- azure - 带有通配符证书的 Aks TLS AGIC 入口
- github-actions-self-hosted-runners - 为什么只有一个 GitHub 自托管 runner 接受新工作?
- python - gRPC HealthCheck 报告 SERVING,即使它被定义为 NOT_SERVING
- python - 如何使用 python 将给定的 PDF 提取到文本和表格并将数据存储在 .csv 文件中?
- mongodb - 如何在 MongoDB 的子文档中添加额外的属性
- c++ - 如果我有一个 cpp 文件目录,如何将它们全部包含在内?
- sql - 将内联查询转换为 TypeORM 中的查询
- c# - C# USB HidDevice 类
- python - Heroku:显示来自回调内部的 PyQt5 元素的 Dash 应用程序不起作用(线程问题?)
- python - 更改对象声明 Python