Ways to display the items in one list within a different list using typescript

How can I display elements from statusList2 within the statusList1 loop?

service.statusList1 contains two elements -> [0], [1]

service.statusList2 also contains two elements -> [0], [1]

 <tr *ngFor="let status1 of service.statusList1; index as i">

      <td>{{status1}}</td>

      <td>{{service.statusList2[i]}}</td>

      {{i+1}}
 </tr>

I am utilizing Angular 6 for this task.

A more traditional JavaScript approach would look something like this:

for (let i = 0; i < someArray.length ; i++) {
  let item = someArray[i];
}

However, I prefer a simpler Angular 6 equivalent using indexing in the loop like so:

<tr *ngFor="let status1 of service.statusList1; index as i">

         <!-- <td>{{status1}}</td> -->          
         <td>{{service.statusList1[i]}}</td>

          {{i+1}}
     </tr>

To see an example, you can visit: https://stackblitz.com/edit/angular-1m31pe Feel free to make any edits or improvements on the example provided.

Answer №1

The 'Hello' component is unnecessary. Everything else appears to be functioning correctly. I made a modification to the app.component.ts as follows:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';

    array1: Array<string> = ["Hi","Hi2"];
  array2: Array<any> = [{name: "No hi"},{name:"no hi2"}];
}

To view the updated Stackblitz, click here.

Answer №2

A simple solution has emerged. Utilizing the 'let' keyword is all that is needed.

 <tr *ngFor="let a of arr1; let i = index">    
    <td>{{a.name}}</td>
    <td>{{arr2[i].name}}</td>
</tr>

The correct implementation can be found here:

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

When using the router to send state, it returns as undefined

My scenario involves a component that utilizes the router to navigate to another component using a boolean variable in the URL: this.router.navigate(['/consume/course/' + this.id, {state: { isFirst }} ], { replaceUrl: true }); In the destination ...

Having trouble uploading SharePoint list item with attachment from Angular to ASP.NET Core Web API via NgModel

Recently, I successfully added a SharePoint list item with an attachment from Angular to ASP.NET Core Web API. This was achieved using FormControl in the Angular application. I found guidance on how to upload files from Angular to ASP.NET Core Web API at ...

generate a new canvas within an Angular 6 framework at a library

I am in the process of developing a unique Angular library that will enable image cropping using canvas. I initialized the library with "ng generate library" command, but when attempting to draw on the canvas, no results are visible. Here is the code from ...

ngFor failing to properly update when new data is pushed

I am currently working on creating an array of reactive forms in Angular. Here is what I have set up: typesForms: FormGroup[] = []; In my HTML, I loop through this array like so: <form *ngFor="let type of typesForms; let i = index" [formGroup]="types ...

Encountering an issue where the module '@angular/compiler-cli/ngcc' cannot be located in Angular 8

Upon attempting to run ng serve from my terminal window, I encountered the following error: An unhandled exception occurred: Cannot find module '@angular/compiler-cli/ngcc' Here is an excerpt from my package.json file: { "name": "ProjectDeta ...

Having difficulty ensuring DayJs is accessible for all Cypress tests

Currently embarking on a new Cypress project, I find myself dealing with an application heavily focused on calendars, requiring frequent manipulations of dates. I'm facing an issue where I need to make DayJs globally available throughout the entire p ...

Switching from lite-server to my own express server.js file for Angular 2: Step-by-step guide

Recently, I decided to take on the challenge of migrating my existing Angular 1.x project to Angular 2. While I wanted to switch to TypeScript and Angular 2 for the client side, I preferred to keep my existing server code intact. However, I encountered an ...

angulartechnology: Error 62: Property 'resolveAndCreate' is not found in 'Injector' type

After updating to ng2 beta 17, I encountered this error: Error:(62, 33) TS2339: Property 'resolveAndCreate' does not exist on type 'typeof Injector'. var injector = Injector.resolveAndCreate( [ TodoService, ...

Using TypeScript, you can utilize RxJS to generate a fresh Observable named "Array" from a static array

I've successfully created an observable from an array, but the issue is that its type shows as Observable<number> instead of Observable<number[]> getUsers(ids: string[]): Observable<number[]> { const arraySource = Observable.from ...

Step-by-step guide on incorporating HTML into a popover within Angular4

After successfully implementing a hover popover in Angular using PopoverModule from ngx-popover, I now need to modify the content inside the popover. My search led me to this example: <ng-template #popContent>Hello, <b& ...

When using veevalidate to reset a form in TypeScript, the form is not fully reset as

When I have a form with veevalidate and submit it, the data is emitted to the parent component, but I struggle with resetting the form. According to the veevalidate guidelines, the form should be reset using $refs. To set up the refs, follow this link: T ...

Encountering an error while setting up @angular/material package - "Data path should not contain additional properties (styles)''

Struggling to install material in my project, despite hours of searching on Google for a solution. I have come across similar problems, but none quite like mine. Upon running ng add @angular/material, an error message is displayed. An unhandled exception ...

Angular 8 project experiencing issues with Bootstrap dropdown functionality

I have successfully installed the packages listed below: npm install --save bootstrap@3 npm install --save popper.js angular-popper npm install jquery --save Afterwards, I included the styles and scripts in the angular.json file in the exact order as sho ...

Nested ControlGroup in Angular2's ControlArray

I've hit a roadblock trying to iterate through a ControlArray that has Controlgroups in a template. In TypeScript, I successfully created the ControlArray and added some ControlGroups by looping over data fetched from an API. The console displays the ...

Setting up ESLint for TypeScript with JSX configuration

I am encountering problems with TypeScript configuration. Below is the code snippet from my tsconfig.json: { "compilerOptions": { "target": "es5", "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLib ...

Google Maps API Version 3 now allows for custom overlays to be hidden when they overlap

I have implemented multiple custom overlays on a map for various cities and I am trying to manage the overlapping ones by hiding or collapsing them. My goal is to display and expand overlays with the highest population whenever there is available space. M ...

Utilizing RavenDB with NodeJS to fetch associated documents and generate a nested outcome within a query

My index is returning data in the following format: Company_All { name : string; id : string; agentDocumentId : string } I am wondering if it's possible to load the related agent document and then construct a nested result using selectFie ...

The custom error handling middleware in Express.js appears to be ineffective for certain error cases

The custom error handler code snippet provided below: export const errorHandler: ErrorRequestHandler = (err, _, res) => { if (err instanceof HttpError) { res.status(err.statusCode).json({ message: err.message }); return; } res.st ...

What is the reason behind TypeScript not narrowing types with control flow and never in the case of arrow functions?

Why does Typescript fail to narrow with a call to fail, but will narrow with a call to fail2? Is this a bug in Typescript? const fail = (message?: string): never => { throw new Error(message); }; function fail2(message?: string): never { throw ...

How can I make ngFor update after an object has been modified?

I'm currently working on a function that updates an object property after an item is dropped in a drag and drop scenario. However, it seems like either my function is incorrect or there might be something else that needs to be done because the display ...