Angular6打开/显示组件按服务 [英] Angular6 open/show component by service

查看:78
本文介绍了Angular6打开/显示组件按服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个角度组件用作我的应用程序的对话框(例如,显示应用程序错误),并创建了一个对话框服务,以从其他组件打开/显示该对话框.

I built an angular component to use as a dialog for my app (e.g to show application errors) and a dialog-service to open/show that dialog from other components.

dialog.component.html

dialog.component.html

<kendo-dialog *ngIf="opened">
  <div>
    Some Content
  </div>
</kendo-dialog>

dialog.compontent.ts

dialog.compontent.ts

import { Component, OnInit } from '@angular/core';
import { Dialog } from './dialog'; // Model

@Component({
  selector: 'dialog',
  templateUrl: './dialog.component.html',
  styleUrls: ['./dialog.component.scss']
})
export class DialogComponent implements OnInit {
  public opened = false;
  public dialog: Dialog; // Contains properties like title, message

  constructor() {
  }

  ngOnInit() {}

  public showDialog(dialog: Dialog) {
    this.dialog = dialog;
    this.opened = true;
  }
}

dialog.service.ts

dialog.service.ts

import { Injectable } from '@angular/core';
import { Dialog } from './dialog';

@Injectable()
export class DialogService {
  constructor() {}

  public showDialog(
    title: string,
    message: string,
    isConfirm: boolean,
    icon?: string
  ) {
    const dialog = new Dialog(title, message, isConfirm, icon);

    // TODO: Open/Show Dialog Component with DialogService
    // set opened property from DialogComponent = true
  }
}

要在任何位置显示我的DialogComponent,我在DialogService中需要做什么? 例如,我在某处有一个try/catch块,想用DialogComponent显示错误消息:

Whats do I need to do in DialogService to be able showing my DialogComponent from anywhere? For example I have a try/catch block somewhere and want to show the error message with DialogComponent:

try {
// Do something
} catch(error => {
    this.dialogService.showDialog('Title', error.Message, true);
})

推荐答案

您应该使用Angular CDK 覆盖.

Your should use the Angular CDK Overlay.

它允许您创建具有一定不透明度的背景,在其上动态注入组件,并配置位置策略和滚动策略.

It allows you to create a backdrop with a certain opacity, to dynamically inject a component over it, and to configure the position strategy and scroll strategy.

让我为您提供一些可以帮助您入门的代码:

Let me provide you some code that may help you get started:

constructor(
   private overlay: Overlay,
   private componentFactoryResolver: ComponentFactoryResolver
) {}

let componentFactory = this.componentFactoryResolver.resolveComponentFactory(DialogComponent);

const overlayRef = this.overlay.create(
  {
    hasBackdrop: true,
    positionStrategy: this.overlay.position().global().centerHorizontally().centerVertically()
  }
);

overlayRef.backdropClick().subscribe(res => {
  overlayRef.detach();
});

let portal = new ComponentPortal(componentFactory.componentType);

let component = overlayRef.attach<DialogComponent>(portal);

component.instance.onCloseClick.subscribe(() => {
  overlayRef.detach();
});

这篇关于Angular6打开/显示组件按服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆