Angular Material:如何相对于元素定位 MatDialog?

2023-11-22

我正在开发一个角度应用程序。我想在单击按钮时打开一个弹出对话框(MatDialog 的实例)。我在主页的方法中执行此操作,如下所示

    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,这样弹出窗口就会显示在窗口的右上角。它做得很好。两天后,我尝试将其放置在按钮下方(顶部:52px),但它不起作用,就像它保持以前的位置一样(在前两天)。你能帮助我吗


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

然后使用MatDialogRef method updatePosition.

主要组件模板

<button #myButton></button>

主要部件

import { AfterViewInit, Component, ElementRef, OnDestroy, ViewChild } from '@angular/core'
import { DialogService } from './dialog.service.ts'

@Component({
  selector: 'main-component',
  templateUrl: 'main.component.html',
  styleUrls: ['main.component.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
      }
    )
  }
}

对话框组件.ts

import { Component, ElementRef, Inject, OnInit } from '@angular/core'
import { MatDialogConfig, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'


@Component({
  selector: 'dialog-component',
  templateUrl: './dialog.component.html',
  styleUrls: ['./dialog.component.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)
  }
}

对话框.service.ts

import { ElementRef, Injectable } from '@angular/core'
import { MatDialog, MatDialogRef } from '@angular/material'

import { DialogComponent } from './dialog.component'


/**
 * 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
  }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular Material:如何相对于元素定位 MatDialog? 的相关文章

随机推荐

  • 在 AngularJS 中拒绝带有多个参数的 Promise(如 $http)

    回调 httpPromise 有多个参数 主体 状态 标头 配置 我想手动创建类似的承诺 但不知道该怎么做 我想做的或多或少是 myservice action then function status message config 我知道
  • IIS7 劫持我的 Coldfusion 错误页面

    在我的异常处理文件中 我将状态代码设置为 404 然后渲染 n 个 HTML 页面 作为错误页面 想想失败鲸鱼
  • 同时安装 Visual Studio 2010 和 VS2008 会导致问题吗?

    这可能会导致什么样的问题 安装在虚拟机里更好吗 并行安装问题始终可能存在 您应该采取适当的保护措施 例如备份 使用虚拟机等 根据我个人的经验 它的效果很好 在我的 Tech Ed 演示中 我使用 Visual Studio 2005 200
  • 如何更改 Java 中 HTTP 响应中的字符集编码

    我必须从远程服务器获取一些 JSON 对象 为此我正在使用这个功能 它工作得很好 除了有时会获取一些奇怪的数据 我相信这是因为它使用 ASCII 字符集进行解码 请在下面找到我正在使用的方法 public HttpResponse call
  • Java 中的双向映射? [复制]

    这个问题在这里已经有答案了 我在 Java 中有一个简单的整数到字符串的映射 但我需要能够轻松地从整数检索字符串 以及从字符串检索整数 我尝试过 Map 但它只能从整数中检索字符串 这是一种方法 private static final M
  • 如何在 ASP.NET Core 中结合 FromBody 和 FromForm BindingSource?

    我创建了一个新的 ASP NET Core 2 1 API 项目 其中包含Datadto 类和此控制器操作 HttpPost public ActionResult
  • 如何获取对象属性的类型提示?

    我想获取对象属性的类型提示 我只能获得该类的提示 而不能获得该类的实例 我尝试过使用foo instance class from here但这只显示了类变量 那么在示例中我如何获得类型提示bar class foo var int 42
  • 在 Genymotion Android 中连接到 VPN

    我正在尝试在 Genymotion 虚拟设备中配置并连接到 VPN 我可以在虚拟设备中配置 VPN 连接 但无法连接 没有显示错误 我在我的 PC 和真实 Android 设备中使用了相同的设置 它在设备和 PC 中运行良好 我认为是 Vi
  • 在单个 RDS 文件中保存多个变量

    我想将变量列表传递给 saveRDS 以保存它们的值 但它会保存它们的名称 variables lt c A B C saveRDS variables file R 它保存单个向量 变量 我也尝试过 save variables file
  • 如何从文件中读取前 n 行和后 n 行?

    如何读取文件的前n行和后n行 For n 2 我读online that head n2 tail n2 会起作用 但事实并非如此 cat x 1 2 3 4 5 cat x head n2 tail n2 1 2 预期输出为n 2将会 1
  • 对多个条件使用 if else 语句

    样本数据 x lt runif 100 min 0 max 1 y lt runif 100 min 0 max 1 dif lt x y dat lt data frame x dif 我想要做的是在数据框中创建另一列dat called
  • 如何在容器内运行 kubectl 命令?

    在 pod 内的容器中 如何使用 kubectl 运行命令 例如 如果我需要在容器内执行类似的操作 kubectl 获取 Pod 我已经尝试过 在我的 dockerfile 中 我有以下命令 RUN curl LO https storag
  • php ajax表单提交而不刷新父页面

    我有一些关于 ajax 表单提交的问题 send on click function ajax type POST url ads process php data ads serialize success function if dat
  • 使用 Java Graphics 进行内部剪辑

    我需要使用 java awt Graphics 绘制一条线 但只应渲染位于矩形之外的线部分 是否可以使用图形剪切支持 或者我是否需要自己计算交集并剪切线 您需要使用Area班级 此示例将演示如何执行您所要求的操作 import java a
  • 将 NIB 文件转换为 XIB 文件

    有没有办法转换NIB文件到XIB文件以便我可以在 Xcode 4 中打开它们 一旦我编辑了它们 有没有办法将它们转换回NIB 一旦 nib 文件被扁平化和剥离 现在这是编译过程的默认部分 那么在 IB 中打开它就很困难 您可能想看看笔尖解锁
  • UTF-8 中的代理字符是什么?

    我有一个奇怪的验证程序 用于验证 utf 8 字符串是否是有效的主机名 PHP 中的 Zend Framework Hostname valdiator 它允许 IDN 国际化域名 它将每个子域与由其十六进制字节表示定义的字符集进行比较 两
  • 干净的番石榴方法来处理可能为空的集合

    我有一个需要参数的方法Collection
  • 手动将 url 与 .NET Core 3.0 中注册的端点进行匹配

    对于我的应用程序 我想将 url 与所有注册的路由进行匹配 以查看是否存在匹配项 当有匹配时 我想从匹配中提取路由值 我在 ASP NET Core 2 1 中得到了这个工作 但我似乎无法按照在 NET Core 3 中检索路由的方式检索路
  • Apache Django Mod_Wsgi - 自动重新加载

    我正在尝试自动重新加载我的 django 应用程序 该应用程序在我的本地 Windows 计算机上使用 apache mod wsgi 我想知道在哪里添加以下文章中引用的代码 http code google com p modwsgi w
  • Angular Material:如何相对于元素定位 MatDialog?

    我正在开发一个角度应用程序 我想在单击按钮时打开一个弹出对话框 MatDialog 的实例 我在主页的方法中执行此操作 如下所示 openDialog event const element document getElementById