Angular叠加层错误将非现有DOM元素视为覆盖层容器。
创始人
2024-10-24 12:01:40
0

这个问题通常是由于在应用中没有正确配置覆盖层容器而引起的。为了解决此问题,可以使用Angular的Overlay服务的create方法来创建一个覆盖层容器,并将其附加到现有的DOM元素。例如:

在组件中创建一个ViewContainerRef和OverlayRef:

import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core'; import { Overlay, OverlayRef } from '@angular/cdk/overlay'; import { ComponentPortal } from '@angular/cdk/portal';

@Component({ selector: 'app-example-overlay', template: , }) export class ExampleOverlayComponent implements OnInit, OnDestroy { @ViewChild('overlayHost') overlayHost: ElementRef; private overlayRef: OverlayRef;

constructor(private overlay: Overlay) {}

ngOnInit() { const positionStrategy = this.overlay .position() .global() .centerHorizontally() .centerVertically();

this.overlayRef = this.overlay.create({
  hasBackdrop: true,
  positionStrategy,
});

}

ngOnDestroy() { this.overlayRef.dispose(); }

showOverlay() { const portal = new ComponentPortal(SomeOverlayComponent); const componentRef = this.overlayRef.attach(portal); // 让覆盖层包含组件的区域 componentRef.instance.overlayHost = this.overlayHost; } }

在覆盖层组件中添加一个Input属性:

import { Component, Input } from '@angular/core';

@Component({ selector: 'app-some-overlay', template:

, }) export class SomeOverlayComponent { @Input() overlayHost: ElementRef; constructor() {}

ngOnInit() { const nativeElement = this.overlayHost.nativeElement; // 将overlayContent附加到overlay

相关内容

热门资讯

详情透视!约局吧透视挂下载!切... 详情透视!约局吧透视挂下载!切实是有辅助教程(有挂分享)-哔哩哔哩1、金币登录送、破产送、升级送、活...
详情透视!约局吧是否有挂!其实... 详情透视!约局吧是否有挂!其实一直总是有辅助插件(有挂规律)-哔哩哔哩1、点击下载安装,约局吧是否有...
必备透视!佛手大菠萝有挂吗!切... 必备透视!佛手大菠萝有挂吗!切实是真的有辅助技巧(有挂方针)-哔哩哔哩1、每一步都需要思考,不同水平...
解迷透视!werplan外卦神... 解迷透视!werplan外卦神器!竟然是有辅助软件(确实有挂)-哔哩哔哩小薇(辅助器软件下载)致您一...
揭幕透视!poker worl... 揭幕透视!poker world辅助!切实有辅助攻略(真实有挂)-哔哩哔哩1、每一步都需要思考,不同...
普及透视!拱趴大菠萝作必弊方法... 普及透视!拱趴大菠萝作必弊方法!竟然一直总是有辅助脚本(有挂总结)-哔哩哔哩1)拱趴大菠萝作必弊方法...
了解透视!fishpoker透... 了解透视!fishpoker透视!本来一直都是有辅助技巧(有挂方法)-哔哩哔哩1、fishpoker...
推荐透视!pokemmo辅助官... 推荐透视!pokemmo辅助官网!确实是真的有辅助方法(有挂透视)-哔哩哔哩1、任何pokemmo辅...
开挂透视!德州圈脚本!一贯是有... 开挂透视!德州圈脚本!一贯是有辅助技巧(讲解有挂)-哔哩哔哩德州圈脚本破解侠是真的助透视。每个模型中...
必备透视!we poker免费... 必备透视!we poker免费辅助器!一直有辅助神器(真的有挂)-哔哩哔哩运we poker免费辅助...