在将其标记为重复之前,请注意这些都不适用于我:question 1 / question 2 / question 3
所以,在收到对前一个question的回复后,我开始使用rxjs跟随guide上所述的所有内容,我提供了
然而,由于未触发订阅方法,我很快再次陷入困境。
我已将我的服务编码为:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import {FormDTO} from "../dto/formDTO.model";
@Injectable()
export class ApiDosageDialogFormCommunicationService {
private formDTO = new Subject<FormDTO>();
//Observable string streams
formDTOpulling$ = this.formDTO.asObservable();
//Service message commands
formDTOpushing(formDTO: FormDTO) {
this.formDTO.next(formDTO);
}
将其从父母发送给孩子,如:
this.apiDosageDialogFormCommunicationService.formDTOpushing(this.formDTO)
它工作正常,直到这里,服务收到DTO,我可以读取它的值,但是当我尝试在模态服务中使用它时,subscribe方法什么都不做,这是我和#的组件39;我试图获取DTO:
import {Component, OnDestroy, OnInit} from "@angular/core";
import {JhiEventManager} from "ng-jhipster";
import {NgbActiveModal} from "@ng-bootstrap/ng-bootstrap";
import {Response} from "@angular/http";
import {ActivatedRoute} from "@angular/router";
import {Observable} from "rxjs/Rx";
import {FormService} from "./form.service";
import {FormDTO} from "../dto/formDTO.model";
import {ApiDosageDialogFirstStepPopupService} from "./apiDosage-dialog-first-step-popup.service";
import {ApiDosageDialogFormCommunicationService} from "./apiDosageDialogFormCommunication.service";
import {Subscription} from "rxjs/Subscription";
@Component({
selector: 'jhi-apiDosage-dialog-first-step',
templateUrl: './apiDosage-dialog-first-step.component.html'
})
export class ApiDosageDialogFirstStepComponent implements OnInit {
formDTO: FormDTO;
isSaving: boolean;
constructor(
public activeModal: NgbActiveModal,
private formService: FormService,
private apiDosageDialogFormCommunicationService: ApiDosageDialogFormCommunicationService,
private eventManager: JhiEventManager
) {
}
ngOnInit() {
this.isSaving = false;
}
clear() {
this.activeModal.dismiss('cancel');
}
save() {
this.isSaving = true;
if (this.formDTO.id !== undefined) {
this.subscribeToSaveResponse(
this.formService.save(this.formDTO));
} else {
this.subscribeToSaveResponse(
this.formService.save(this.formDTO));
}
}
private subscribeToSaveResponse(result: Observable<FormDTO>) {
result.subscribe((res: FormDTO) =>
this.onSaveSuccess(res), (res: Response) => this.onSaveError());
}
private onSaveSuccess(result: FormDTO) {
this.eventManager.broadcast({ name: 'journalListModification', content: 'OK'});
this.isSaving = false;
this.activeModal.dismiss(result);
}
private onSaveError() {
this.isSaving = false;
}
}
@Component({
selector: 'jhi-apiDosage-first-step-popup',
template: ''
})
export class ApiDosageDialogFirstStepPopupComponent implements OnInit, OnDestroy {
routeSub: any;
formDTO: any;
subscription: Subscription;
constructor(
private route: ActivatedRoute,
private apiDosagePopupService: ApiDosageDialogFirstStepPopupService,
private apiDosageDialogFormCommunicationService: ApiDosageDialogFormCommunicationService
) {
}
ngOnInit() {
this.subscription = this.apiDosageDialogFormCommunicationService.formDTOpulling$.subscribe(
formDTO => {
console.log('In the component' +this.formDTO.sample.id),
this.formDTO = formDTO;
},
error => {
alert('Made it all the way here: '+this.formDTO);
});
this.routeSub = this.route.params.subscribe((params) => {
this.apiDosagePopupService
.open(ApiDosageDialogFirstStepComponent as Component, this.formDTO);
});
}
ngOnDestroy() {
this.routeSub.unsubscribe();
}
}
请注意,在上一课程中,我尝试在onInit和构造函数中添加subscribe方法。
上一个类调用模态的open方法,这是模态服务的源代码:
import { Injectable, Component } from '@angular/core';
import { Router } from '@angular/router';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import {FormService} from "./form.service";
import {FormDTO} from "../dto/formDTO.model";
import {Observable} from "rxjs/Observable";
@Injectable()
export class ApiDosageDialogFirstStepPopupService {
private ngbModalRef: NgbModalRef;
constructor(
private modalService: NgbModal,
private router: Router,
private formService: FormService
) {
this.ngbModalRef = null;
}
open(component: Component, formDTO?: Observable<any> | any): Promise<NgbModalRef> {
return new Promise<NgbModalRef>((resolve, reject) => {
const isOpen = this.ngbModalRef !== null;
if (isOpen) {
resolve(this.ngbModalRef);
}
if (formDTO) {
// this.formService.find(id).subscribe((journal) => {
// this.ngbModalRef = this.journalModalRef(component, journal);
// resolve(this.ngbModalRef);
// });
setTimeout(() => {
this.ngbModalRef = this.apiDosageFirstStepModalRef(component, formDTO);
resolve(this.ngbModalRef);
console.log(formDTO);
}, 0);
} else {
//setTimeout used as a workaround for getting ExpressionChangedAfterItHasBeenCheckedError
setTimeout(() => {
this.ngbModalRef = this.apiDosageFirstStepModalRef(component, new Observable<any>());
resolve(this.ngbModalRef);
}, 0);
alert('no form');
}
});
}
apiDosageFirstStepModalRef(component: Component, formDTO: Observable<any>): NgbModalRef {
const modalRef = this.modalService.open(component, { size: 'lg', backdrop: 'static'});
modalRef.componentInstance.formDTO = formDTO;
modalRef.result.then((result) => {
this.router.navigate([{ outlets: { popup: null }}], { replaceUrl: true, queryParamsHandling: 'merge' });
this.ngbModalRef = null;
}, (reason) => {
this.router.navigate([{ outlets: { popup: null }}], { replaceUrl: true, queryParamsHandling: 'merge' });
this.ngbModalRef = null;
});
return modalRef;
}
}
答案 0 :(得分:1)
正在发生的事情是在组件订阅之前调用formDTOpushing
。这意味着组件将不会收到该值。订阅Subject
的组件仅接收订阅后发出的值。 (他们最初订阅时没有收到价值)
要让组件在订阅时接收先前发布的值,请使用BehaviorSubject或ReplaySubject代替