Utilizing the power of Promise and async operators in conjunction with Angular's ng

Update:

I'm happy to report that my alternative solution is now also functional. It seems there was a glitch in the local storage that was causing the previous issue.

Query:

I am seeking guidance on how to effectively use the combination of promise, async pipe, and ngfor. Despite attempting the following implementation, I have been unable to achieve success.

html

 <ion-item *ngFor="let c of contacts | async">
        <ion-label fixed>c.name</ion-label>
 </ion-item>

ts

  contacts: Promise<Contact[]>;
  constructor() {
    this.getContacts()
  }

  getContacts() {
    this.contacts = this.storage.get('contacts').then((val: Contact[]) => {
      console.log('log', val);
      return val;
    });
  }

Answer №1

Give this a shot in your .ts file:

fetchContacts() {
   this.contacts = this.storage.fetch('contacts');
}

This method allows the async pipe in your HTML to subscribe to the returned promise.

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

Resolving a Form Group Challenge in Angular 9

Disclaimer: I am still learning about angular and TypeScript in general. My goal is to create an activation component that can submit a code directly to the service if it's included in the URL parameter query. If the code is not in the URL, then the ...

Is it possible to apply a style change to several components at once using a single toggle switch

I am looking to implement a day/night feature on my web app, triggered by a simple toggle click. While I can easily add this feature to a single component using the navigation menu, I am faced with the challenge of incorporating it into multiple component ...

Find all documents in Angular Firestore that are related to a specific document_REFERENCE

Seeking a way to search through all documents in collection a that have a reference to a specific document in collection b. Despite my efforts, I couldn't find a solution here or on Google :/ This is what I tried in my service class: getAsFromB(id ...

[code=access-denied]: Access denied for this project resource

Encountering a firebase error: https://i.sstatic.net/IVq7F.png In my project using react-hooks-firebase, with react and TypeScript, I encountered an issue after adding true to the firebase database... https://i.sstatic.net/5gQSD.png firebase.ts: After ...

TypeScript definition for a dispatcher within a map

I am in the process of creating a typed function that will utilize an object based on its key: const commandOrQuery = { CREATE_USER_WITH_PASSWORD: CreateUserCommandHandler, GET_USERS: GetUsersQueryHandler, }; The structure of commandOrQuery is as foll ...

Issue encountered post compilation of App: ag-grid using Angular 6

Currently, I am utilizing ag-grid with Angular 6 and so far everything is functioning smoothly. However, after building my application using ng build, the build process is successful. Yet, upon running my app, an error occurs: ERROR TypeError: Cannot re ...

Having trouble disabling an ESLint rule for ESLint, TypeScript, Vue, and WebPack Encore?

I've been delving into Webpack Encore and ESLint issues for quite some time now, but unfortunately, I haven't been able to find a solution to my problem. Specifically, I've been trying to change or disable certain TypeScript-ESLint rules, b ...

Ways to conceal a fabric button

Is there a way to hide my Material button properly? The button appears grey and works fine: <button mat-raised-button class="mat-primary" (click)="deleteClick()" [disabled]="data.createMode"> <mat-icon>delete_forever</mat-icon>DELET ...

Find all Mondays occurring within a specified date range using Moment.js

I need to extract all Mondays within a specific date range. let start = moment(this.absence.FromDate); let end = moment(this.absence.ToDate); The user has the option to deactivate certain weekdays during this period by setting booleans. monday = true; t ...

IntelliJ not capturing interface with imported reference path

Directory Layout frontend | static | ts | react |components AnotherFile.d.ts Index.d.ts Situation While implementing AnotherFile.d.ts and Index.d.ts, there are no errors flagged by IntelliJ for the following code: ...

A pattern matching algorithm that verifies a series of port numbers (ranging from 1 to 65535) spread out across

I am in search of a regular expression that can accurately identify valid port numbers (ranging from 1 to 65535) within a text area. The input format will resemble the following: 80 80 25 53 110 --- --- This pattern will continue across multiple lines, wi ...

Components in Angular 5 app hosted on Firebase fail to load

I successfully deployed my Angular 5 app to Firebase using the commands firebase init and firebase deploy. By default, the main page is pulled from the index.html file within the public directory. To ensure that my index.html was displayed, I made a chan ...

How to detect changes in a variable within a different component using Angular 7

My navbar component includes a dropdown menu that allows users to change the language using ngx/translate: <div class="traduction"> <ul> <li class="nav-item dropdown no-arrow"> <a class="nav-link dropdown ...

Unable to install Angular to the most recent version

Can anyone help me with installing Angular 16? I've tried various solutions on GitHub and Stack Overflow, but I always end up with Angular 15.2.9. This problem occurs on my Windows 11 machine, while on my MacBook, I have successfully installed Angula ...

What's the best way to display a component once a function has been executed?

My service controls the visibility of components using *ngIf! Currently, when I click a button, the service sets to true and the components appear instantly! I want the components to only show after a specific function has finished executing. This means I ...

The React useState Props error message TS2322: Cannot assign type 'string' to type 'number'

I'm attempting to pass Props to React useState Hooks. Both of my props are required and should be numbers, but I keep receiving a Typescript error stating: Type 'string' is not assignable to type 'number'. TS2322 However, I am ...

Angular 2 NgMatchers in TypeScript: Understanding Semantic Errors

I have been working on setting up an Angular 2 project using Gulp and Jasmine (as seen in this example). However, I keep encountering semantic errors while compiling my typescript code: matchers.d.ts(4,37): error TS2503: Cannot find namespace 'jasmin ...

Troubleshooting TypeScript window augmentation not functioning in individual modules

I would like to extend the window object with a new property. This can be achieved by adding the following code: // global.d.ts import { IConfig } from './src/models'; export {}; declare global { interface Window { _env: IConfig; ...

Angular Drag and Drop - Dragging incorrectly orientated when element is rotated

After rotating an element with transform:rotate(90deg), I noticed that when I try to drag the element, it does not move in the expected direction. This issue arose while using angular material drag and drop cdk. To demonstrate this problem, you can visit ...

What is the significance of the code statement in the Angular ng2-table package?

Could you please explain the functionality of this specific code line? this.rows = page && config.paging ? this.changePage(page, sortedData) : sortedData; ...