我正在尝试实现基本(非常基本)的模式实现。我有一个ModalService
和一个ModalComponent
。
ModalService
创建ModalComponent
的实例,并使用@ angular / cdk / portal将其注入页面。
我可以使模式显示得很好:-)
ModalComponent
具有一个我希望ModalService
订阅的Observable属性,以便在模式中单击“关闭”按钮时,可以发出一个值,并且{{1} }可以关闭模式。
但是,当组件发出该值时,服务不会对其执行操作。从服务方面看,我已经订阅了,但从组件方面看,它显示了0个观察者。
我想也许我可以使用典型的ModalService
EventEmitter,但由于在子类元素最初并不存在,因此我不确定在modal类中将其挂钩。
我在想也许我的组件参考不太正确(也许我有两个不同的参考?)。我正在尝试this answer
的建议有什么想法我在做什么错吗?
服务
@Output()
组件
export class ModalService {
private modalPortal: ComponentPortal<any>;
private bodyPortalHost: DomPortalHost;
constructor(private componentFactoryResolver: ComponentFactoryResolver,
private appRef: ApplicationRef,
private injector: Injector) {
}
showModal(modalName: string) {
// set up the portal and portal host
this.modalPortal = new ComponentPortal(ModalComponent);
this.bodyPortalHost = new DomPortalHost(
document.body,
this.componentFactoryResolver,
this.appRef,
this.injector);
// display the component in the page
let componentRef = this.bodyPortalHost.attach(this.modalPortal);
// listen for modal's close event
componentRef.instance.onModalClose().subscribe(() => {
console.log('I will be very happy if this this gets called, but it never ever does');
this.closeModal();
});
// console.log(componentRef.instance.onModalClose()) shows 1 subscriber.
}
closeModal() {
this.bodyPortalHost.detach();
}
}
此外,如果我碰巧问错了问题,这意味着有更好的整体方法,我欢迎提出建议。 :-)
谢谢!
答案 0 :(得分:2)
此代码对我来说几乎不需要更改,因此对于您的问题感到困惑。首先,我使用ng new
使用Angular CLI创建了一个项目。然后,我使用instructions on their site安装了ng材料。
我创建了一个与您的模态服务相同的模态服务
import {ApplicationRef, ComponentFactoryResolver, Injectable, Injector} from '@angular/core';
import {DomPortalHost, ComponentPortal} from "@angular/cdk/portal";
import {ModalComponent} from "./modal/modal.component";
@Injectable({
providedIn: 'root'
})
export class ModalService {
private modalPortal: ComponentPortal<any>;
private bodyPortalHost: DomPortalHost;
constructor(private componentFactoryResolver: ComponentFactoryResolver,
private appRef: ApplicationRef,
private injector: Injector) {
}
showModal(modalName: string) {
// set up the portal and portal host
this.modalPortal = new ComponentPortal(ModalComponent);
this.bodyPortalHost = new DomPortalHost(
document.body,
this.componentFactoryResolver,
this.appRef,
this.injector);
// display the component in the page
let componentRef = this.bodyPortalHost.attach(this.modalPortal);
// listen for modal's close event
componentRef.instance.onModalClose().subscribe(() => {
console.log('I will be very happy if this this gets called, but it never ever does');
this.closeModal();
});
// console.log(componentRef.instance.onModalClose()) shows 1 subscriber.
}
closeModal() {
this.bodyPortalHost.detach();
}
}
我创建了一个模态组件。 TypeScript与您的相同:
import { Component, OnInit } from '@angular/core';
import {Observable, Subject} from "rxjs/index";
@Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit {
constructor() { }
ngOnInit() {
}
private modalClose: Subject<any> = new Subject();
onModalClose(): Observable<any> {
return this.modalClose.asObservable();
}
closeModal() {
// console.log(this.onModalClose()) **shows zero observers** :-(
this.modalClose.next();
this.modalClose.complete();
}
}
您没有为我们提供HTML的模型,但这是我所使用的:
<p>
modal works!
</p>
<button (click)="closeModal()">Close Modal</button>
这是我的app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import {ModalService} from "src/app/modal.service";
import { ModalComponent } from './modal/modal.component';
@NgModule({
declarations: [
AppComponent,
ModalComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule
],
providers: [ModalService],
bootstrap: [AppComponent],
entryComponents: [ModalComponent]
})
export class AppModule { }
请注意,我在entryComponents和ModalService中将ModalComponent定义为提供程序。
app.component HTML:
<button (click)="showModal()">Show Modal</button>
app.component打字稿:
import { Component } from '@angular/core';
import {ModalService} from "./modal.service";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
constructor(public modalService: ModalService){}
showModal(){
this.modalService.showModal("This is the modal name");
}
}
我将ModalService注入到构造函数中,并响应主应用程序中的按钮单击调用了showModal方法。
单击按钮,将显示模式。它看起来不像模态,但这可能是因为缺少样式:
现在单击模式内的关闭按钮:
您看到模态消失了,控制台输出显示了您的消息。
这篇文章是否可以帮助您找到错过的消息?
要添加的一件事。如果要将数据从模态中发送到调用它的组件,则必须更改modalComponent的closeModal方法:
closeModal() {
// console.log(this.onModalClose()) **shows zero observers** :-(
this.modalClose.next('Your Data Here, it can be an object if need be');
this.modalClose.complete();
}
您的模态服务可以使用onModalClose().subscribe()
方法访问数据:
componentRef.instance.onModalClose().subscribe((results) => {
console.log(results);
console.log('I will be very happy if this this gets called, but it never ever does');
this.closeModal();
});