Retrieve an array that is returned from a function in TypeScript

In this function, I am trying to access the value of the 'value' array outside the function in the same class. This function is being called in a setter method where the ID is set like so: setId(id:string){ this.onChange(id)}

class ModalShowComponent implements OnInit {
  onChange(id) {
    this.xyzService.getCodes(id).subscribe(
      list => {
        var value = [];
        list.forEach((object) => {
          value.push(object.payload.val());
        });
        // some code
        return value;
      });
  }
}

Answer №1

Here is a possible solution:

class DisplayModal implements OnInit {
  public data:[]= [];

  onSwitch(id) {
    this.modalDataService.fetchDetails(id).subscribe(
      info => {
        info.map((obj) => {
          this.data.push(obj.payload.val());
        });
      });
  }
}

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

Is it true that the Angular 2 Injector provides a fresh instantiation each time it is called?

During a unit test, I am utilizing the injector and spying on the http object in this manner... beforeEach(async(inject([MyRepository, MockBackend, Http],(myRepository: MyRepository, backend: MockBackend, http : Http) => { spyOn(http,'get&ap ...

Cancel all uncompleted axios requests and start fresh

I am currently utilizing the axios library for handling API requests. I find myself in a situation where I need to cancel all ongoing or pending requests and initiate new ones using a different API. I have attempted the following code: async getOldRespon ...

Leveraging Express.js alongside websockets

I am currently working on an application that is built on Expressjs and Angularjs. I am looking to incorporate a few two-way calls in addition to the existing HTTP calls. While I have gone through some websocket chat tutorials, none of them seem to be inte ...

Embedding JQuery within a PHP file

Recently, our inventory web page was coded in PHP by someone else. I've been working on integrating a jQuery function into the webpage that displays a description whenever a barcode is scanned. While testing on jsbin.com everything works perfectly, bu ...

Having Trouble with Angular Form Reset and Receiving the Error Message "Trying to Reference a Destroyed View: detectChanges"

Upon successfully saving a value, I trigger a form reset. This involves calling a service method to send data to an API. this.customerService.saveSupplier({ customerId: Context.customerId, supplier: supplier }).subscribe(res => { this.pageReset(); ...

D3 bar chart displaying identical values but with varying data sets

<!DOCTYPE html> <html lang='en'> <head> <meta charset="UTF-8"/> <title>Interactive Bar Chart using D3</title> <script src="https://d3js.org/d3.v4.min.js"></script> </head> <b ...

AngularJS input range is written inside the bubble that crosses over the screen

Is there a way to write inside the bubble of the range slider? I have adjusted the design of the range slider and now I simply want to display the number inside the bubble: Please see image for reference I aim to display the number inside a circle. What ...

Having trouble generating a readable output bundle due to the following error message: "The entry module cannot be found: Error: Unable to resolve './src/index.js'"

I'm currently working with Webpack version 6.14.8 within an Asp.net Core Razor Pages project in Visual Studio 2019. My objective is to generate a readable output file, and here's the directory structure I've set up: |-->wwwroot -----> ...

Issue: ASSERTION ERROR: token must be declared [Expecting => null is not undefined <=Actual]

I encountered an error while working on my project. The only special thing I did was use oidc(openId) for authentication. I made some changes to the bootstrap project and now the first component that is running is the home-main component, which includes t ...

Tips for recognizing the initial page load during the current execution of the JavaScript program

I am working on an ASP.net MVC project and I need to create a JavaScript function that will only execute on the initial load of the page during the current program run. If someone navigates to a different page and then returns, I do not want the function t ...

Angular does not support having multiple Subject listeners subscribed at the same time

Within the Angular framework, I am faced with a scenario where two components are both actively listening to an RXJS Subject within a shared service. These components are loaded concurrently, each residing in its own separate tab. The issue I encounter is ...

What steps can be taken to remove the search parameter responsible for the error?

Imagine having a webpage that displays search results based on the parameters in the URL, like this: https://www.someurl.com/categories/somecategory?brands=brand1,brand2,brand3 This URL will show listings for only brand1, brand2, and brand3. Additionally ...

Node appears to be struggling to find the cors

I added the cors package and confirmed that it's inside the node_modules directory. However, I keep encountering this error message. /usr/src/app/node_modules/ts-node/src/index.ts:859 server | return new TSError(diagnosticText, diagnosticCodes, ...

See CoffeeScript from the perspective of Compiled JavaScript

https://i.sstatic.net/JRNjG.png I recently started learning Coffeescript and decided to install the 'BetterCoffeeScript' package on Sublime. I am able to see the syntax highlighting, but I'm struggling to view my compiled javascript code. W ...

What is the best way to display or conceal an array object using a toggle switch?

I'm trying to implement a show/hide functionality for descriptions when toggling the switch. Additionally, I want the switch to be initially checked and only show the description of each respective result. However, my current code is displaying all de ...

Tips for including markers on Google Maps once the map has finished loading:

Currently, I am developing a phoneGap application and going around my house to gather coordinates: var locations = [ {lat:123.12313123, lng:123.123345131}, {lat:123.12313123, lng:123.123345131} ] While referring to the documentation here, everything ...

Why isn't my JavaScript AJAX PHP if statement functioning properly?

I have been struggling with this issue for more than two hours now and cannot seem to find a logical solution. When I remove the If statement highlighted by --> this arrow, the alert() function works perfectly fine. It triggers when I simply use if(true ...

What is the process for migrating a Node-based OpenLayers project to utilize the local ol file?

I have developed a web application using OpenLayers. Initially, I incorporated OpenLayers via npm and everything was functioning properly. However, due to new requirements arising, I now need to eliminate all node dependencies and replace them with local ...

Looking to execute a PHP script upon form submission

I have a form that I need to submit in order for a PHP script to run using AJAX. <form method="post" action="index.php" id="entryform" name="entryform"> <input type="submit" name="submit" value="Submit" onclick="JavaScript:xmlhttpPost('/web/ ...

Tips for managing boolean values in a JSON data structure

My JSON object is causing issues because it has True instead of true and False instead of false. How can I fix this problem? The code snippet below shows that obj2 is not working properly due to boolean values being capitalized. <!DOCTYPE html> < ...