0
我有自举模式,其中我保存了一些数据,保存后我想在文本框中选中这些数据。 Modal在子组件中,并加载我已经在应用程序模块的entryComponents中提到它。角度2服务之间的沟通,孩子和父母不工作?
现在为了在这种情况下进行通信,我添加了服务将管理组件之间的通信。
这里我的服务
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class EmitterService {
private events = new Subject();
private eventSetSource = new Subject<any>();
private eventCompletedSource = new Subject<any>();
// Observable string streams
eventSet$ = this.eventSetSource.asObservable();
eventCompleted$ = this.eventCompletedSource.asObservable();
// Service message commands
set(obj: any) {
this.eventSetSource.next(obj);
}
}
这里是莫代尔/子组件:
import { EmitterService } from 'app/shared-services/emitter/emitter.service';
@Component({
selector: 'app-short-organization',
templateUrl: './short-organization.component.html',
styleUrls: ['./short-organization.component.css'],
providers: [SessionService, ContactOrganizationService, ToastService, EmitterService]
})
export class ShortOrganizationComponent extends DialogComponent<ConfirmModel, boolean> implements ConfirmModel, OnInit {
title: string;
message: string;
public _formGrp: FormGroup;
constructor(dialogService: DialogService,
private _emitterService: EmitterService
) {
super(dialogService);
_emitterService.eventCompleted$.subscribe(x => {
console.log(x);
})
}
ngOnInit() {
}
onSubmit() {
let _userDetail = this.sessionService.getCurrentUser();
var obj = { id: 0, value: 'xyz'};
this._emitterService.set(obj);
}
父组件:
import { Component, OnInit,OnDestroy } from '@angular/core';
import 'rxjs/add/operator/takeUntil';
import { Subject } from 'rxjs/Subject';
import { ShortOrganizationComponent } from 'app/modules/manage-organization/short-organization/short-organization.component';
import { EmitterService } from 'app/shared-services/emitter/emitter.service';
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.css'],
providers: [
EmitterService
]
})
export class ContactComponent implements OnInit,OnDestroy {
private ngUnsubscribe: Subject<void> = new Subject<void>();
constructor(
private _emitterService: EmitterService
) {
debugger
this._modalSubs = _emitterService.eventSet$.subscribe(
x => {
debugger;
console.log(x);
this.onSaveSelect(x);
},
error=>{
debugger;
console.log(error);
}
);
}
ngOnInit() {
}
onSaveSelect(val) {
debugger
}
ngOnDestroy(): void {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
}
}
我在这里呼吁使用ng2-bootstrap-modal dailog服务这个子组件:
openOrganizationModal() {
let disposable = this._dialogService.addDialog(ShortOrganizationComponent, {
title: 'Confirm title',
message: 'Confirm message'
}, { backdropColor: 'rgba(23, 19, 19, 0.5)' })
.subscribe((x) => {
console.log(x)
});
}
此代码正在我的父组件中加载我的子组件,并且在某些按钮单击时调用此方法。
在设置值我的订阅代码在父母没有被调用。这里有什么遗漏吗? ****我从应用程序复制它
好的。我认为在父母中提及它会更好,因为在第二种情况下,它被添加到@NgModule中将使它为所有父亲孩子创建单个实例(如果我将它用作通信服务),并且在这种情况下设置值,但是说child1对于parent1将可用于parent2。我对吗?或者我想像一个小白菜。 –
保持@NgModule中的EmitterService正常工作,但只在Parent中保持不起作用。正如我前面提到的我使用entryComponents加载子组件是不是因为它而工作?我不确定在这里 –
不确定'entryComponents'是什么意思,你用viewContainerRef.createComponent()来创建它吗?我需要看到更多细节才能分辨出发生了什么。如果这个组件实际上不是'提供者:[...]'中有服务的组件的子组件,那它就无法工作,它是如何作为孩子添加的并不重要。 –