Use the ngFor directive to iterate over the most recently created array from the parent ng

I am looking to link material tabs with ngFor to generate arrays for child ngFor.

Let's start from the beginning:

<mat-tab-group>
<mat-tab *ngFor="let tab of asyncTabs ">
    <ng-template mat-tab-label>{{tab.label}}</ng-template>
    <div *ngFor="let event of tabs">
        <event-thumbnail [eve]="event"></event-thumbnail>
    </div>  
</mat-tab>

The first step I take is to create a "tabs" labels

        this.asyncTabs = [
        {label: 'A-B', content: this.getTabContent(10)},
        {label: 'C-D', content: this.getTabContent(5)},
        {label: 'E-F', content: this.getTabContent(0)},
        {label: 'G-H', content: this.getTabContent(0)},
        {label: 'I-J', content: this.getTabContent(0)},
        {label: 'K-L', content: this.getTabContent(0)},
        {label: 'M-N', content: this.getTabContent(0)},
        {label: 'O-P', content: this.getTabContent(0)},
        {label: 'R-S', content: this.getTabContent(0)},
        {label: 'T-U', content: this.getTabContent(0)},
        {label: 'W-X', content: this.getTabContent(0)},
        {label: 'Y-Z', content: this.getTabContent(0)},
    ]

My getTabContent() function filters an array based on a condition and returns tabs array with filtered objects.

    getTabContent(condition){
     this.tabs = this.events.filter(x=> x.price == condition)
     return this.tabs 
    }

After that, I want to iterate through the child ngFor *ngFor="let event of tabs" to populate the content of each tab

The issue is that the second ngFor always takes the last tabs array created by the first ngFor, resulting in all tabs being populated with the same data

New Array configuration tabs: IEvent[] = []

Can anyone provide assistance with this, please? P.S. I am aware that list filtering can be used, but I prefer to do it this way

Answer №1

Instead of using this.tab, you can simply utilize tab.content. Each time the getTabContent function is executed, it will update the values of this.tab, ensuring that it always contains the most recent value.

<mat-tab-group>
<mat-tab *ngFor="let tab of asyncTabs ">
    <ng-template mat-tab-label>{{tab.label}}</ng-template>
    <div *ngFor="let event of tab.content">
        <event-thumbnail [eve]="event"></event-thumbnail>
    </div>  
</mat-tab>
getTabContent(condition){
   return this.events.filter(x=> x.price === condition);
}

Example: https://stackblitz.com/edit/angular-m3w1pv

Answer №2

To specify the specific "tab" you wish to iterate through using an index:

<mat-tab-group>
<mat-tab *ngFor="let tab of asyncTabs; let i = index">
    <ng-template mat-tab-label>{{tab.label}}</ng-template>
    <div *ngFor="let event of tabs[i]">
        <event-thumbnail [eve]="event"></event-thumbnail>
    </div>  
</mat-tab>

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

What are the steps to send a Firebase cloud message using typescript?

I'm looking for guidance on how to send a Firebase Cloud Message through my Firebase Functions backend. I seem to be running into trouble with the payload and need help resolving it. Do I need an interface for the payload? Thank you in advance! Error ...

Filtering an array of parent elements in Javascript based on a specific value found in

Trying to filter data from a 3-layer array based on child array values. Making progress but hitting a roadblock at the end. { "id": 1, "grandparent": "Timmy", "parents": [ { "id&q ...

Convert JSONX data into the standard JSON format

I have extracted XML data from my database and am looking to convert it to JSON format. Utilizing an IBM DataPower database, the process involves converting the XML to JSONx before using IBM's default translator to further convert it into JSON. My cu ...

What causes the inconsistency in TypeScript's structure typing?

It is well-known that TypeScript applies structure typing, as demonstrated in the following example: interface Vector { x: number; y: number; } interface NamedVector { x: number; y: number; name: string; } function calculateLength(v: Vecto ...

When trying to import a module in Angular, an error is encountered while using Scully

Exploring the utilization of Scully within my Angular project has presented me with a challenge. In Angular, Modules must be imported in order to use them effectively. However, when attempting to execute Scully through the command line: npm run scully --sc ...

Guide to Conditionally Importing a Module in Angular

I am currently developing a module for Search integration. Instead of directly importing the SearchModule inside my app.module.ts file, I would like to implement a method where an API is called and the SearchModule is imported based on the API response. @N ...

Deliver a numerical input to the component on an equivalent hierarchical tier

I need to find a way to pass the values of the "things" array to another component on the same level in my app. The structure of my app is as follows: sidebar.component data.service body.component In the sidebar component, I have a button that triggers a ...

Which location can I find my 'bundles' folder in Angular 2, NPM, and Visual Studio 2015?

Each guide mentions /node_modules/angular2/bundles/... I'm puzzled why I can't find the 'bundles' directories for Angular or any library obtained from NPM within Visual Studio 2015? Downloaded from NPM "dependencies": { "angular ...

The ASP.NET Core Web API is designed to handle incoming dates that are one day in the past, as sent by

After selecting a date from an Angular material datepicker, the ASP.NET Core Web API consistently receives the date as one day earlier. The date being sent is obtained from a form control and assigned to a property like so: scheme.date1 = this.formControl ...

Somehow, my array only manages to traverse exactly half of its elements

Something odd is happening with my input tag in the HTML file where only half of my array elements are being processed. The input collects numbers/letters and assigns a line of code, like this: let result = Object.fromEntries( Object.entries(answers2).m ...

The UploadFile Interface seems to be missing

Can someone clarify whether the @UploadedFile decorator's interface is predefined or if I need to define it myself? ...

Tips for populating the header of an angular-material card

I am working with an angular-material classic card that has the following template: <mat-card class="example-card"> <mat-card-header> <mat-card-title>Shiba Inu</mat-card-title> </mat-card-header> <mat-card-conten ...

When attempting to pass an array of objects to a child component, TypeScript raises an error regarding types

Hey everyone, I'm currently facing an issue while trying to pass an array of objects as props. Here's the content of my data.json file: [ { "category": "Reaction", "score": 80, "icon": " ...

Discovering if objects possess intersecting branches and devising a useful error notification

I have 2 items that must not share any common features: const translated = { a: { b: { c: "Hello", d: "World" } } }; const toTranslate = { a: { b: { d: "Everybody" } } }; The code ab ...

How can I effectively address process.on test in TypeScript Mocha Testing with the help of a Sinon Spy?

I need to conduct a test on the warning process for my Typescript project. The specific code that I am attempting to test is shown below: process.on('warning', (warning) => { LoggingService.info('Warning, Message: ' + warning.mes ...

Exploring how Jasmine tests the checkbox change handler function in the latest version of Angular (12)

I'm attempting to create a basic test function for a checkbox change event, but I'm encountering some issues running it. The error message states "Spec has no expectations," and the handler function is not included in code coverage. Template: &l ...

Exploring Iframes within Angular2

import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: `<h1>Greetings, {{name}}!</h1> <iframe src="http://example.com/Home?requestId=+[testRequestId]+" allowfulls ...

Utilizing ES6 array methods to convert multidimensional arrays into chart-ready data

Seeking help with converting an array to a specific data format for chart display. The chrart.js library requires data in the following format: dataset = [ { label: 'one', data: []}, {label: 'two', data: []} ]; I ...

Unforeseen outcomes when duplicating array elements in C

I have been attempting to replicate elements of an array by using a basic copy function located outside of the main function. The code I have written is as follows: int* copy(int* nums){ int size = sizeof(nums) / sizeof(nums[0]); // determining array l ...

Experiencing issues with test timeouts in Angular while trying to log in

I'm having trouble writing an Angular e2e Protractor test. Every time I try to test a login with the correct email and password, I keep getting a timeout error. describe("login view", () => { const app = new App(); const login = new L ...