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

相关内容

热门资讯

重大消息!聚财平台辅助(辅助挂... 【福星临门,好运相随】;重大消息!聚财平台辅助(辅助挂)wepoker透视有用吗(起初真的有挂);暗...
玩家必备攻略!科乐游戏辅助脚本... 玩家必备攻略!科乐游戏辅助脚本视频(辅助挂)竞技联盟破解版最新版(本来真的是有挂)是一款可以让一直输...
详细说明!乐达app辅助(辅助... 详细说明!乐达app辅助(辅助挂)aapoker免费透视脚本(往昔有挂);小薇(透视辅助)致您一封信...
记者发布!jj斗地主外开挂(辅... 记者发布!jj斗地主外开挂(辅助挂)德普之星透视辅助软件激活码(从前存在有挂);1、很好的jj斗地主...
一分钟了解!广东雀神祈福真的有... 一分钟了解!广东雀神祈福真的有用吗(辅助挂)wepoker好友房开挂(先前真的有挂);1、超多福利:...
重大来袭!吉安中至小程序(辅助... 重大来袭!吉安中至小程序(辅助挂)wepoker永久免费脚本(确实真的有挂);人气非常高,ai更新快...
我来向大家传授!微信小程序多功... 您好:微信小程序多功能辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
大家学习交流!途游大作战辅助(... 您好:途游大作战辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特...
攻略讲解!花舍辅助(辅助挂)w... 您好,花舍辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏...
每日必看!老k麻将辅助器(辅助... 【福星临门,好运相随】;每日必看!老k麻将辅助器(辅助挂)wepoker免费脚本咨询(本来是真的有挂...