Tips on implementing nested ngFor loops

I am managing a Cloud Firestore database with a unique structure:

  • service

    • [serviceId]

      • [userId]

        • documentId

          • service_img : "test.png"
  • users

    • [uid]

      • services

        • [documentId]

          • name: "user added service name"

The above showcases the basic collection/document/field setup within my DB.

My goal is to retrieve all "service_img" (added in the "userId" collection) when executing a particular service using ngFor. However, my current code causes a browser crash due to continuous looping. Here is the code snippet I am working on:

html

<div *ngFor="let x of userservicelist">
    <div class="makup_nm">{{x.name}}</div>
     <div class="imageDiv">
       <img src="assets/imgs/add.png" (click)="addserviceImg(x.id)" />
    <img src="{{y.service_img}}" *ngFor="let y of getSerImgs(x.id)" />
    </div>
</div>

typescript

getuserServices(){
   this.userservicelist=[]
   let db = firebase.firestore()
   db.collection(`users/${this.userId}/services`).get().then((res)=> 
       res.forEach((service) => {
          let temp;
          temp = service.data();
          temp.id = service.id; 
          this.userservicelist.push(temp);
          console.log(this.userservicelist)
       })
     )
   console.log(this.userservicelist)
}

getSerImgs(serviceId){
    this.serviceimage=[]
    let db = firebase.firestore();
    db.collection(`service/${serviceId}/${this.userId}`).get().then((res)=>{
      res.forEach(service => {
      let temp = service.data();
        console.log(temp)
        temp.id = service.id; 
        this.serviceimage.push(temp);
      });
    })
    return this.serviceimage
}

Answer №1

Employ a nested component for complex nesting with *ngFor

You could also experiment with a recursive component approach

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

Unable to utilize material tabs in this situation

Discovering the material tabs feature at https://material.angular.io/components/tabs/api#MatTab got me excited to implement it in my project. After adding the suggested import, I encountered an issue where I couldn't find the module "@angular/materia ...

The error message "Property is not found on type 'Object'" suggests that the property being accessed does not

I wrote a function called getAll getAll<T>() { return this.http.get(`${environment.apiUrl}/products`); } Here is how I am invoking it: this.productService.getAll() .pipe(first()) .subscribe(products => { debugger let s ...

Modify the style of an element using a media query and Angular 4

Is there a way to update a class of an element based on the browser's width in my .ts file? matchMedia('(max-width: 400px)').addListener((mql => { if (mql.matches) { this.myclass = 'toggled'; } })); In the HTML, it shou ...

TS1128: End of file reached without declaration or statement

I am currently working on a TypeScript/React project and encountering an issue. It has been a while since I last worked with React, so I might be a bit rusty. After following some documentation, I created this component but am facing a TS1128 error (Decla ...

What is the best method for calculating the total sum by multiplying the values in an array?

In my current project, I have an array consisting of multiple objects, each containing a property named "amount". My goal is to sum up all these amount values to get the total. Initially, I attempted to use a for loop but encountered an issue where settin ...

Type inference and the extends clause in TypeScript 4.6 for conditional types

My focus was on TypeScript 4.7 when I created the following types: const routes = { foo: '/foo/:paramFoo', bar: '/bar/:paramFoo/:paramBar', baz: '/baz/baz2/:paramFoo/:paramBar', } as const; type Routes = typeof routes; ...

Guide on integrating an element into a different element in a Vue 3 Tree Viewer

In my current setup, I've implemented a TreeView component that holds a tree. Each tree entry includes Children with their own unique label, perm, and further children. Take a look at an example of the tree: App.vue let tree = ref({ label: 'o ...

Is there a more efficient method in Typescript to define the types of an object containing arrays?

Suppose I have an object that contains various roles, each granting a specific set of rights: const allRoles = { user: [ 'right1' ], admin: [ 'right1', 'right2' ], } as const If I want to define the types ...

No elements present in TypeScript's empty set

Question for discussion: Can a type be designed in TypeScript to represent the concept of an empty set? I have experimented with defining one using union, disjoint union, intersection, and other methods... ...

Adjust the size of the mat-expansion indicator to your desired height and width

Trying to modify the width and height of the mat indicator has been a bit challenging. Despite following suggestions from other similar questions, such as adjusting the border width and padding, I am still unable to see the changes reflect in my CSS file ...

Retrieve Laravel User from a Fetch Request

Attempting to extract the active user from a fetch request to my backend. Here is my front-end code: let apiToken: string | null = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); fetch('http:// ...

Change an array of objects into a map where each object is indexed by a unique key

I'm attempting to transform an array of objects into a map, with the index based on a specific attribute value of the object in typescript 4.1.5 Additionally, I am only interested in attributes of a certain type (in this case, string) A similar ques ...

Struggling with the TypeScript generic syntax for the GroupBy function

Struggling to figure out where I'm going wrong with this TypeScript signature after spending some time on it. I've been working on a group by function: const group = <T>(items: T[], fn: (item: T) => T[keyof T]) => { return items.re ...

Angular and Bootstrap Styles: A Tale of 404_errors

Encountering a strange issue with the Boostrap css file returning a 404 error. Just a basic Angular "quick-start" style app created using the Angular CLI. Added Bootstrap to package.json and installed it via npm update. The index file is straightforward, ...

A guide on showcasing nested arrays data in an Angular application

info = [ { list: [ { title: 'apple'} ] }, { list: [ { title: 'banana'} ] } ] My goal here is to extract the list items. Here is how they are structured. desired r ...

Avoid invoking a TypeScript class like a regular function - _classCallCheck prevention

I am currently developing a TypeScript library that needs to be compatible with all versions of JavaScript. I have noticed that when calling a class in TS without using new, it does not compile properly, which is expected. In ES6/Babel, a class automatica ...

Ways to simulate a variable imported in the module being tested without it being a function parameter can be achieved by using describe.each and changing the mock value for each test

I have a requirement to test a function within my TypeScript module. module-to-test.ts import { config } from './app-config'; export const isSomethingWhatINeedSelector = createSelector( firstDependencySelector, secondDependencySelector ...

How can I ensure that my `if` statement functions properly upon visiting the website?

I have implemented the following code in my template: <mat-tab-group mat-align-tabs="start" (selectedTabChange)="onScrollDown($event)"> And this is the corresponding code in my component: onScrollDown(evt:any ) { ...

The final position of the Angular increment animation does not hold

Is there a way to trigger an animation when the value of countAllOrders changes? Specifically, I am attempting to adjust the margin of a list using keyframes in an Angular animation. Here is my HTML code: <ul class="digits" [@ordersValue]=&q ...

Verify whether a component is a React.ReactElement<any> instance within a child mapping operation

I am facing a challenge with a component that loops through children and wraps them in a div. I want to exclude certain elements from this process, but I am struggling to determine if the child is a ReactElement or not (React.ReactChild can be a string or ...