how can I access or get the value of this.dealType outside this.trasactionService.transactionEvent$. suscription ? . I wanted to access the data and value ofthis.dealType outside , maybe someone can help.
it has value inside the this.trasactionService.transactionEvent$.subscrib but I want to access it outside , I tried logging it using console logs its empty
Thank you, appreciated.
#code
export class DealsApprovalComponent implements OnInit {
dealType: string;
totalDealsForApproval = 0;
constructor(
private dealService: DealService,
private notificationService: NotificationService,
private trasactionService: TransactionService,
private route: Router,
private dealTransactionService: DealTransactionService
)
{
}
ngOnInit(): void {
this.trasactionService.transactionEvent$.subscribe((data) => {
if(data) {
switch (data['operation']) {
case 'deal/deal-type':
this.dealType = data["dealType"];
break;
}
}
},
(error) => {}
);
console.log("this.dealType" , this.dealType)
}
The console log shows empty because it is executed first before the code inside the subscribe method. If you need to do something with "this.dealType", you can just create a method after you assigned it like so.
ngOnInit(): void {
this.trasactionService.transactionEvent$.subscribe((data) => {
if(data) {
switch (data['operation']) {
case 'deal/deal-type':
this.dealType = data["dealType"];
this.doSomething();
break;
}
}
},
(error) => {}
);
}
private doSomething() {
console.log(this.dealType);
}
Also, remember to unsubscribe when needed to avoid memory leaks.