callingmodal.component.ts
export class CallingmodalComponent implements OnInit {
openDialog(): void {
const dialogRef = this.dialog.open(SharemodalComponent, {
width: '640px', disableClose: true
});
console.log(dialogRef);
}
}
sharemodalComponent.component.ts
export class SharemodalComponent implements OnInit {
onaddCus(): void {
console.log(this.sharingWith);
console.log(this.addCusForm.value.selectedPermissionType);
}
}
只要单击一个按钮,我就调用onaddCus()。在这个函数中,我想将this.sharingWith和this.addCusForm.value.selectedPermissionType传递给callingmodal.component.ts。
如何将这些参数传递给callingmodal.component.ts?
发布于 2020-06-19 09:37:20
我假设您是在动态地从sharemodalComponent
创建callingmodal
。在这种情况下,您可以使用单例服务在它们之间共享数据。尝试以下几点
shared.service.ts
@Injectable({providedIn: 'root'})
export class SharedService {
private _sharingWithSource = new BehaviorSubject<any>(null);
private _selectedPermissionTypeSource = new BehaviorSubject<any>(null);
public sharingWith$ = this._sharingWithSource.asObservable();
public selectedPermissionType$ = this._selectedPermissionTypeSource.asObservable();
set sharingWithSource(value: any) {
this._sharingWithSource.next(value);
}
set selectedPermissionTypeSource(value: any) {
this._selectedPermissionTypeSource.next(value);
}
}
sharemodalComponent.component.ts
export class SharemodalComponent implements OnInit {
constructor(private sharedService: SharedService) { }
onaddCus(): void {
this.sharedService.sharingWithSource = this.sharingWith;
this.sharedService.selectedPermissionTypeSource = this.addCusForm.value.selectedPermissionType;
console.log(this.sharingWith);
console.log(this.addCusForm.value.selectedPermissionType);
}
}
在调用模式组件中,在ngOnInit
钩子中订阅以捕获所有推送通知到可观测值。
callingmodal.component.ts
export class CallingmodalComponent implements OnInit, OnDestroy {
completed$ = new Subject<any>();
constructor(private sharedService: SharedService) { }
ngOnInit() {
this.sharedService.sharingWith$.pipe(
takeUntil(this.completed$)
).subscribe(
value => {
if (value) { // <-- default value is `null`
this.sharingWith = value
}
}
);
this.sharedService.selectedPermissionType$.pipe(
takeUntil(this.completed$)
).subscribe(
value => {
if (value) {
this.selectedPermissionType = value
}
}
);
}
openDialog(): void {
const dialogRef = this.dialog.open(SharemodalComponent, {
width: '640px', disableClose: true
});
console.log(dialogRef);
}
ngOnDestroy() {
this.completed$.next(); // <-- close the observables
this.completed$.complete();
}
}
最新情况-解释
BehaviorSubject
--多播可观测RxJS Subject
的变体。它可以保存推送给它的当前数据,并在订阅时立即发出它。这就是为什么它需要一个初始值(我们在这里使用null
),并在赋值之前进行检查。next
方法被用来将一个新的值推到可观测值上。其他推送通知的有效方法是error
和complete
。推到error
将调用观察者中的错误回调,调用complete
将完成可观察的流。OnDestroy
钩子中的组件中的订阅始终是很好的做法。一种方法是将订阅分配给局部变量并在其上调用unsubscribe()
。其他优雅的方法是使用RxJS takeUntil
操作符。有关此方法的更多信息,这里。asObservable()
函数的一个很好的解释。发布于 2020-06-19 09:26:38
将以下构造添加到SharemodalComponent中:
constructor(@Inject(MAT_DIALOG_DATA) public data: any,
public dialogRef: MatDialogRef<SharemodalComponent>) {}
超过您可以从您的CallingmodalComponent传递数据并在关闭事件上订阅:
const dialogRef = this.dialog.open(SharemodalComponent, {
width: '640px', disableClose: true,
data: { sharingWith: '100px', ... }
});
dialogRef.afterClosed().subscribe(result => {
console.log(result );
});
如果单击按钮,您可以关闭对话框并将数据传递给CallingmodalComponent:
this.dialogRef.close('some data here');
https://stackoverflow.com/questions/62475180
复制