简体   繁体   English

模态窗口关闭功能不起作用

[英]The modal window close function does not work

I made a simple project on angular.我做了一个关于角度的简单项目。 In this project, I implemented a modal window.在这个项目中,我实现了一个模态窗口。

My problem is that when the modal window is open, the function that closes the window does not work, but when I click on the button again to open the modal window, it closes.我的问题是当模态窗口打开时,关闭窗口的功能不起作用,但是当我再次单击按钮打开模态窗口时,它关闭了。 And even when I call the close function from the open function, it works too.即使我从 open 函数调用 close 函数,它也能工作。

Why does the modal window close function not work?为什么模态窗口关闭功能不起作用?

Link to the created project链接到创建的项目

https://stackblitz.com/edit/angular-zmytwj https://stackblitz.com/edit/angular-zmytwj

constructor(private overlay: Overlay, private viewContainerRef:ViewContainerRef) {}

Should add viewContainerRef when initialize component初始化组件时应添加 viewContainerRef

ngOnInit() {
    this.overlayRef = this.overlay.create({ hasBackdrop: true });
    this.formComponentPortal = new ComponentPortal(FormComponent, this.viewContainerRef);
  }

And your close button event还有你的关闭按钮事件

 public onButtonClose() {
     this.overlayRef.detach();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM