角材料:如何相对于元素定位MatDialog?

编程入门 行业动态 更新时间:2024-10-25 12:16:28
本文介绍了角材料:如何相对于元素定位MatDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个有角度的应用程序.当我单击按钮时,我想打开一个弹出对话框(MatDialog的实例).我在主页上按以下方法进行操作

I am developping an angular application. I want to open a dialog pop up (an instance of MatDialog) when I click on a button. I do it in a method of my main page as the following

openDialog(event) { const element = document.getElementById(event.target.id); const jqelement = $(element); const position = jqelement.position(); // cache the position const bottom = position.top + jqelement.height(); const dialogConfig = new MatDialogConfig(); dialogConfig.disableClose = true; dialogConfig.autoFocus = true; dialogConfig.position = { top: '' + bottom, right: '0' }; dialogConfig.width = '50%' ; dialogConfig.height = '350px' ; console.log(dialogConfig); this.dialog.open(UserDialogComponent, dialogConfig); }

我希望它位于我单击的按钮的右侧和下方.一开始,我将top:0px放到了窗口的右上角.做得很好.两天后,我尝试将其定位在按钮下方(顶部:52像素),但它无法正常工作,就好像它保持了先前的位置(在前两天)一样.你能帮我吗

I want it to be positioned on the right and underneath the button that I click. At the beginning, I put top: 0px so the pop up displayed on the right up corner of the window. It did it well. After two days I tried to position it just below the button (top: 52px), but it does not work, just as if it keep the previous position (during the first two days). Can you help me

推荐答案

MatDialog弹出窗口可能相对于元素定位.在下面的示例中,根据按钮的边界客户端矩形,在单击的按钮的下方和左侧打开对话框.可以通过模板引用变量来引用该元素.

The MatDialog popup may be positioned relative to an element. In the example below, the dialog is opened below and to the left of the clicked button based on the button's bounding client rectangle. The element may be referenced via a template reference variable.

然后使用 MatDialogRef 方法updatePosition.

<button #myButton></button>

主要组件

import { AfterViewInit, Component, ElementRef, OnDestroy, ViewChild } from '@angular/core' import { DialogService } from './dialog.service.ts' @Component({ selector: 'main-component', templateUrl: 'mainponent.html', styleUrls: ['mainponent.css'] }) export class MainComponent implements AfterViewInit, OnDestroy { @ViewChild('myButton', { static: false }) public myButtonRef: ElementRef constructor(private dialogService: DialogService) {} public openDialog() { dialogRef = this.dialogService.openDialog({ positionRelativeToElement: this.myButtonRef, has_backdrop: true }) this.dialogRef.afterClosed().subscribe( () => { ... this.dialogRef = null } ) } }

dialogponent.ts

import { Component, ElementRef, Inject, OnInit } from '@angular/core' import { MatDialogConfig, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog' @Component({ selector: 'dialog-component', templateUrl: './dialogponent.html', styleUrls: ['./dialogponent.css'] }) export class DialogComponent implements OnInit { private positionRelativeToElement: ElementRef constructor(public dialogRef: MatDialogRef<DialogComponent>, @Inject(MAT_DIALOG_DATA) public options: { positionRelativeToElement: ElementRef }) { this.positionRelativeToElement = options.positionRelativeToElement } ngOnInit() { const matDialogConfig = new MatDialogConfig() const rect: DOMRect = this.positionRelativeToElement.nativeElement.getBoundingClientRect() matDialogConfig.position = { right: `10px`, top: `${rect.bottom + 2}px` } this.dialogRef.updatePosition(matDialogConfig.position) } }

dialog.service.ts

import { ElementRef, Injectable } from '@angular/core' import { MatDialog, MatDialogRef } from '@angular/material' import { DialogComponent } from './dialogponent' /** * Service to create modal dialog windows. */ @Injectable({ providedIn: 'root' }) export class DialogService { constructor(public dialog: MatDialog) { } public openDialog({ position_relative_to_element, user, has_backdrop = false, height = '135px', width = '290px' }: { positionRelativeToElement: ElementRef, hasBackdrop?: boolean, height?: string, width?: string }): MatDialogRef<DialogComponent> { const dialogRef: MatDialogRef<DialogComponent> = this.dialog.open(DialogComponent, { hasBackdrop: hasBackdrop, height: height, width: width, data: { positionRelativeToElement: positionRelativeToElement } }) return dialogRef } }

更多推荐

角材料:如何相对于元素定位MatDialog?

本文发布于:2023-10-08 02:58:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:相对于   元素   材料   MatDialog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!