angular - 如何在触发按钮上方放置一个对话框?

标签 angular angular-material

我正要用拳头砸穿我的电脑屏幕。我有一个对话框,它拒绝在屏幕底部的提交按钮上方移动一英寸——我希望它位于最顶部。

我已确定我的主题正在加载。这在这里得到验证: ide loading of CSS

我试过:

this.dialogBox.open(thankYouModal, {position: {top: '0%', left: '20%'}});

我试过为顶部设置负数和正数。我得到的最好的结果是我可以将模式进一步向下移动,这样用户必须向下滚动才能关闭它。然而,那个笨蛋不会让提交按钮上的毛发动过

我试图将样式表放入组件中,如下所示:

div#cdk-overlay-0.cdk-overlay-container{
    position: fixed !important;
    left: 20%;
    top: 0%;
    background-color: white;
    pointer-events: auto;
}

我无法让这个东西在提交按钮上方移动一英寸。如果有人帮助我弄清楚我做错了什么,我会给他们我的第一个 child 。

(是的,我知道我很戏剧化,但我已经与之抗争并在网上搜索了 3 个小时;我是一个失败的人……)

编辑

这是 thankYouModalComponent 代码:

import {Component} from "@angular/core";
import {MdDialogRef, MdDialogConfig} from "@angular/material";

@Component({
             selector: 'thank-you-modal',
             template: `
                                <h3>Thank You for Your Submission</h3>
                                <p>Your Feedback has been Saved.</p>
                                <button md-raised-button (click)="dialogRef.close()">Close</button>

                        `,
            styles: [`
                        .md-dialog-container {
                                            position: fixed !important;
                                            left: 20%;
                                            top: 0%;
                                            background-color: white;
                                            z-index: 100000;
                    }`]
})

export class ThankYouComponent{
    constructor(public dialogRef: MdDialogRef<any>, ) {}
}

组件的调用方法和构造函数如下:

constructor(@Inject(FormBuilder) fb : FormBuilder,
                 private feedbackService: feedbackService,
                 private dialog : MdDialog){

-------<irrelevant code here>------------

}



    submitFeedback(group : FormGroup){
            const feedback = new Feedback(  group.get('nameBox').value,
                                            group.get('productBox').value,
                                            group.get('upsBox').value,
                                            group.get('downsBox').value);

            if(confirm("Is the data entered what you want to submit?")){
                this.dialog.open(ThankYouComponent, {position: {top: '0%', left:'20%'}});

    }

表单底部的结果对话框

Resulting dialog box at the bottom of the form despite requesting is show at top

最佳答案

更好的方法是注入(inject)按钮元素并将其传递到对话框中。这样,对话框就有了关于如何调整大小/定位的完整上下文(并利用 element.getBoundingClientRect() 函数):

对话框组件:

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

@Component({
  selector: 'app-dialog-component',
  templateUrl: './dialog.component.html',
  styleUrls: ['./dialog.component.scss']
})
export class MyDialogComponent implements OnInit {
  private readonly _matDialogRef: MatDialogRef<MyDialogComponent>;
  private readonly triggerElementRef: ElementRef;
  constructor(_matDialogRef: MatDialogRef<MyDialogComponent>,
              @Inject(MAT_DIALOG_DATA) data: { trigger: ElementRef }) {
    this._matDialogRef = _matDialogRef;
    this.triggerElementRef = data.trigger;
  }

  ngOnInit() {
    const matDialogConfig: MatDialogConfig = new MatDialogConfig();
    const rect = this.triggerElementRef.nativeElement.getBoundingClientRect();
    matDialogConfig.position = { left: `${rect.left}px`, top: `${rect.bottom - 50}px` };
    matDialogConfig.width = '300px';
    matDialogConfig.height = '400px';
    this._matDialogRef.updateSize(matDialogConfig.width, matDialogConfig.height);
    this._matDialogRef.updatePosition(matDialogConfig.position);
  }
  cancel(): void {
    this._matDialogRef.close(null);
  }
}

用法:

onShowDialog(evt: MouseEvent): void {
  const target = new ElementRef(evt.currentTarget);
  const dialogRef = this._matDialog.open(MyDialogComponent, {
    data: { trigger: target }
  });
  dialogRef.afterClosed().subscribe( _res => {
    console.log(_res);
  });
}

关于angular - 如何在触发按钮上方放置一个对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43476958/

相关文章:

javascript - TypeScript Angular - 将字符串添加到数字属性会导致值为零 (0)

javascript - 使用 Typescript 时在外部库中使用公共(public)变量和接口(interface)

html - 分页在 Angular 7 中不起作用。(我使用 Angular Material )

angular - 为应用程序中的所有输入设置默认指令

html - 选择垫选项( Angular )时如何调用方法?

Angular6 模式表单动态选择

angularjs - 尝试使用 Angular Material 实现自动完成芯片。获取无法读取未定义的属性 'nextTick'

javascript - 无法绑定(bind)到 'routerLink',因为它不是已知的 native 属性

Angular2 webpack 应用程序在构建后丢失 Assets

angular - 下拉菜单不会再次打开