What is the process for obtaining the result of an asynchronous function triggered by an event in HTML?

I am currently working on an Angular application where I have a button that triggers a click event. The method being called is asynchronous and involves subscribing to an observable. My goal is to call player.start() only after the listItems.getData() method has completed its execution. However, the challenge lies in the fact that getData() is invoked in the HTML template, making it difficult to catch the completion event and then trigger the start() method.

In player.html:

<button color="primary" (click)="listItems.getData()" ...

In ListItems.ts:

export class ListItems {
    ...
    getData() {
        /**
        ** Since getData() is invoked in the HTML context, capturing the result inside a TypeScript class becomes challenging
        **/
        ...
        return this.http.get(url).subscribe(res => {
            this.data = res;
        }
    }
}

In player.ts:

export class Player {
    constructor(public listItems: ListItems) {
    }
    start() {
        /**
        ** The start method should only be executed once listItems.data has been populated with data
        **/
        let allData = this.listItems.data.replace('\n', '');
        ....
    }
    ....
}

Answer №1

When a ListItem completes an http request, it can send an event (such as onData) to its parent component. The Player component can then associate the "start()" method with the ListItem component's "onData" event.

import { Component, EventEmitter, Output } from "@angular/core";

@Component({
  selector: "list-items",
  template: `
    <h2>Items:</h2>
    <ul>
    <li *ngFor="let item of data">{{ item }}</li>
    </ul>
    <button (click)="getData()">Get Data</button>
  `,
})
export class ListItems {

  @Output()
  dataReceived = new EventEmitter<void>();

  data: string[] = [];

  getData() {
    setTimeout(() => {
      // simulate http request
      this.data = ["hello", "world"];
      this.dataReceived.emit();
    }, 2000);
  }
}


@Component({
  selector: "app-root",
  template: `
    <h1>Player</h1>
    <list-items (dataReceived)="start()"></list-items>
    <h2>start() invoked: {{ invoked }} </h2>
  `,
  styleUrls: ["./app.component.css"]
})
export class Player {
  title = "CodeSandbox";

  invoked: boolean = false;

  start() {
    this.invoked = true;
  }
}

Check out the live example here: https://codesandbox.io/s/angular-angular?fontsize=14&hidenavigation=1&theme=dark

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

Understanding File Reading in Angular/Typescript

I'm currently developing an app similar to Wordle and I'm facing an issue with reading words from a file. Here's what I tried: import * as fs from 'fs'; const words = fs.readFileSync('./words.txt', 'utf-8'); con ...

Does performing regForm.reset() not only clear the form but also deactivate it?

When I use .reset() to reset my form, it works as expected. However, after resetting, when I try to type in the input fields again nothing happens and they don't accept any inputs. <form (ngSubmit)="onFormSubmit(regForm)" #regForm="ngForm"> ...

What is the best way to invoke a function in a specific child component from its parent component?

It seems that I might have provided too much information, but the main question remains: how can I call a method in the child component from the parent template's click() event. <button(click)='get()'>GET</button> In my case, th ...

Is it feasible to select which modules to be loaded into the application?

Looking for a solution to my problem outlined in the title. For example, I am tasked with creating two separate versions of an app - one for France and one for the UK. In some areas, they require completely different implementations. Is it feasible to sw ...

Having trouble linking tables to Node.js with TypeScriptyntax?

I am facing an issue with mapping multiple entities using sequelize. I keep encountering the error message " Error: Profesor.hasOne called with something that's not a subclass of Sequelize.Model". How can I resolve this issue? Below is the code for t ...

What is the mechanism behind angular2's spa routing functioning seamlessly without the need for the hash character in the url?

During my experience with the Angular2 "Tour of Heroes" tutorial, I made an interesting observation about how their single page application router functions without a trailing hash symbol (#) in the URL. This differs from the Kendo SPA router, which typica ...

Encountering difficulty importing a module from a different module within Nuxt

Within my Nuxt project directory, there exists a specific folder named modules which houses my customized modules. For this illustration, it includes the modules foo and bar. The inclusion of foo in the nuxt.config.js file appears as follows: // nuxt.confi ...

Validating specific controls in an Angular 2 FormGroup and marking them as invalid

Currently, I have implemented an Angular 2 Reactive form using FormGroup. The main requirement is to compare the values of two fields, for which I have included a validator in the FormGroup during creation. The validation process seems to be working effect ...

Passing properties to a component from Material UI Tab

I have been attempting to combine react-router with Material-UI V1 Tabs, following guidance from this GitHub issue and this Stack Overflow post, but the solution provided is leading to errors for me. As far as I understand, this is how it should be implem ...

Utilizing the useSelect hook in Typescript to create custom types for WordPress Gutenberg, specifically targeting the core/editor

As I delve into development with WordPress and the Gutenberg editor, my goal is to incorporate TypeScript into the mix. However, I encounter a type error when trying to utilize the useSelect() hook in conjunction with an associated function from the core/e ...

Issues with the radio-inline class in Angular and Bootstrap causing functionality errors

Trying to align my radio buttons on one line using the radio-inline class from bootstrap. Here's my code: <label>Fattura a carico di </label> <label class="radio-inline control-label"><input type="radio" value="banca" n ...

The FOR UPDATE clause is not functioning as intended in the SELECT query

I have been working on isolating my database to prevent multiple servers from reading or updating data in the same row. In order to achieve this, I have structured my query like so: SELECT * FROM bridge_transaction_state as bridge WHERE bridge.state IN (&a ...

Button for liking and disliking with Angular, Node.js, and

On my Twitter-esque website, I am developing YouTube-style (like-dislike) buttons. However, when it comes to implementing these like-dislike buttons using Angular, Node.js, and MYSQL with NgFor loop and ngIf conditions, I encountered a problem. My database ...

Leveraging Angular OpaqueToken for Injecting Config Object Does Not Display Type Errors

When using an OpaqueToken to inject a config object into the application in Angular, I followed the documentation and was able to successfully DI the config into a component and retrieve values. However, I encountered an issue when trying to enforce type c ...

Is the environment file in Angular adequately protected from potential breaches?

Currently utilizing Angular 15, I have included confidential information such as passwords and secret keys for encryption in the environment file. My concern is not about the security of the environment file in the repository (such as git or etc), but rath ...

How to access the component instance in Angular through router events

I am currently working on incorporating a title service into my Angular 10 application. My goal is to subscribe to router events, access the activated route's component, check if it has a title() getter, and then use that information to set the page&a ...

Surprising Outcome when Dealing with Instance Type - Allowing extra properties that are not explicitly stated in the type

I've come across an issue with the InstanceType Utility Type. I am trying to create a function that takes a class constructor and an instance of that class. For instance: export type Class<T=any> = { new(...args: any[]): T; }; function acceptC ...

Guide to adding information to a table with the help of an "interface" in Angular 6 and utilizing Typescript

Looking for some guidance with Angular as I am just starting out. I am currently trying to send an API request in angular 6 using Typescript. However, I am facing difficulties when it comes to adding data to a table. Check out my code on: Codepen In my p ...

The "my-app" selector in Angular2 did not find any elements upon initial loading

Has anyone encountered this error upon the initial page load in their Angular2 app? It disappears upon navigating through the app, but sometimes reappears if I refresh with CTRL+F5. The routing structure of my app is as follows: AppComponent.ts LoginCom ...

Expanding the capabilities of generic functions in TypeScript using type variables

When working with generics in TypeScript, it is often beneficial for a function that accepts generically-typed arguments to have knowledge of the type. There exists a standard approach to achieve this using named functions: interface TestInterface<T> ...