How to combine and return an observable and a value simultaneously in an Angular service using RxJS

Within my Angular application, I am utilizing the ngx-bootstrap modal component.

This modal is being used to provide observable callbacks when the modal is shown and hidden (onShown / onHidden) after a button click event.

The code snippet for this functionality is as follows:

import {Injectable, TemplateRef} from '@angular/core';
import {BsModalService} from 'ngx-bootstrap/modal';
import {BsModalRef} from 'ngx-bootstrap/modal/bs-modal-ref.service';

@Injectable()
export class MyService {

  modalRef: BsModalRef;
  constructor(private modalService: BsModalService) {}

  openModal(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template ,{ class: ' modal-lg' }  );
  }

  onModalShown(){
    return this.modalService.onShown;
    // I WANT TO RETURN this.modalService.onShown + modalRef

  onModalHidden(){
    return this.modalService.onHidden;
  }
}

My main goal is to find out how to combine the onModalShown and onModalHidden methods in order to attach the reference to the modal modalRef at the same time as receiving the response from this.modalService.onShown.

I WOULD LIKE TO RETURN this.modalService.onShown + modalRef and then be able to subscribe to it within my component to retrieve both sets of data.

@Component()
export class MyComponent {

constructor( private myService : MyService) {}

 whenCallbackFired(){
  this.myService.onModalShown.subscribe(()=>{
      HOW CAN I ACCESS BOTH SETS OF INFORMATION HERE??
  })
 }

}

It's important to mention that the modalRef is assigned after certain button clicks.

Any suggestions on how to achieve this?

Answer №1

Is this code sufficient for your needs:

onModalShown() {
  return this.modalService.onShown.pipe(
    map((directive) => ({ directive, modalRef: this.modalRef })
  );
}

In your component, you can then use the following code:

whenCallbackFired(){
  this.myService.onModalShown.subscribe(({ directive, modalRef })=> {
    console.log(modalRef);
  })
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Populating form inputs with FCKeditor content prior to form submission

Currently, I am working on resolving an issue with a form on a website that relies on javascript for field validation. It has come to my attention that certain fields utilize fckeditor, causing the form field values to remain unset until the submit button ...

Verify user identity before sending directory in Express

I'm encountering an issue with authenticating users before they access an express directory file tree. While I can successfully authenticate users on all other pages, I'm facing difficulties with authentication on "/dat/:file(*)" even though I ha ...

Utilizing WordPress Variables Beyond the Loop

Following the update of my loop to gather all necessary data using this code: <?php $json = ""; if ( have_posts() ) { while ( have_posts() ) { the_post(); global $wpdb; $query = "SELECT * FROM ". $wpdb-&g ...

Is it possible to change the ajax src attribute without initiating a new request?

My goal is to create an iframe using a system that utilizes HTTP authentication. Below is the code I am currently working with: <iframe id="dashboard"></iframe> <script type="text/javascript"> $.ajax( ...

Enhance the firstLevel Tree component with Angular Material

I recently developed a multi-level tree in Angular with 3 levels. Currently, when the tree is loaded, only the first level is opened by default. Check out the demo here My goal is to enable users to add or delete items within this tree, and whenever an a ...

Using class-validator in Node.js to validate arrays of objects

My goal is to verify the Alcohol ID and Alcohol Name for emptiness. Below is the format I am working with: { "barId": "string", "barName": "string", "drinksItems": [ { "alcoholId": "string", "alcoholName": "string", "mixerLis ...

Using the .forEach method in JavaScript to convert JSON properties into HTML content

For my project, the JSON data is stored in the variable this.responseText: { 'nav': '<a href="">a</a><a href="">b</a>', 'content': '<div>tartalom</div>', ...

Ensure that the injected service's constructor has completed before running tests in Karma 4 with Angular 7

I'm including a service in this manner: it('test name', inject([ Service], (hcs: Service) => { const pipe = new MyPipe(hcs); const expectedResult = ... //The constructor of the hcs-service must be completed before executing t ...

Accessing class functions in Angular 2 and Ionic

I'm currently using Ionic with Angular 2 to develop an app. However, I'm facing an issue with the code below as I am unable to access the function leaveGroup(group) within the Dialog promise. leaveGroupTapped(event, group) { this.dialogs.con ...

The CSS Grid I've been using is functioning properly on Chrome, but encountering issues on Firefox

:root { --text-color:#fff; } * { padding: 0; margin: 0; border: 0; box-sizing: border-box; } body { margin: 10px; background: #ddd; font-family: 'Noto Serif', serif; } .container { margin: 0 auto; width: ...

The typings for object properties in Typescript

I recently encountered a function call in my code: var myVar = myFunction({ property: 'prop', functionProperty() { console.log(this.property); }, functionProperty2() { this.functionProperty(); } }); I' ...

Is it possible to transmit data from Node.js back to the client?

As a developer, I am faced with the challenge of sending a POST request to Node.js from the client. In the process, I find myself making multiple HTTP POST requests within Node.js to gather and process JSON data. The ultimate goal is to return this process ...

Is there a way to display a message box on initial page load only, using JavaScript or jQuery?

I am looking to display a message box only once when a webpage first loads, and then not show it again if the user refreshes the page. The message should appear regardless of whether the user is logged in or not. Is using cookies the only option for achie ...

How can you display a doughnut chart with a custom color to represent empty or no data, including doughnut rings?

I have integrated a doughnut chart from chartjs into my Vue project using vue-chartjs. Currently, the doughnut chart does not display anything when there is no data or all values are empty. Is there a way to customize the color and show the entire doughnu ...

Is there a way to extract the numerical value from a string within an ID of a div and transform the rest of the string into a variable?

Looking to extract the final id of a div and convert it to a variable. <div class="margin_bot" id="itemRows2"> <p id="rowNum1">...</p> <p id="rowNum2">...</p> <p id="rowNum3">...</p> <p id="rowNum4"> ...

Having trouble animating a collapse element in Bootstrap 5 that was dynamically created using JavaScript

My objective is to develop a collapsible element that notifies the user about the success or error of sending form data to the server. This will provide clear feedback to the user after form submission. function sendForm(event, form) { const notif ...

Encountering an error: "Unhandled promise rejection SyntaxError: Unexpected character in JSON data at line 1 column 1."

When the submit button is clicked, my registration form data is sent using an event function called postData() in React. The user data is sent at the register route, and I have mentioned line numbers in the comments: const postData = async(event) =>{ ...

After the child promise is resolved, have the parent function return a boolean value

I am currently faced with a challenge in my framework where the parent function must output either true or false>. However, I also need to perform an asynchronous operation within that function.</p> <p>How can I ensure that the parent functi ...

angular-cli: Select templates based on the current environment

Currently, I am utilizing @angular/cli: 1.0.0 and aiming to utilize component templates based on the environment. The code implementation is as follows: import {Component} from '@angular/core'; import {environment} from '../environments/env ...

Creating an overlay that dynamically renders components

Trying to create a service that displays a spinner progress while loading, but encountering an error in the console. Here is the service: @Injectable({ providedIn: 'root' }) export class LoadingLockDataService { public message: string; pu ...