Displaying multiple items horizontally using ngFor

I am having trouble organizing my data using the *ngFor loop. I would like to have three items displayed per row when using ngFor. I attempted to use indexing, but it only resulted in one item per row.

<div class="container">
  <div class="row">
    <div class="mb-3 card-deck text-center">
      <div class="card mb-4 box-shadow" *ngFor="let product of item.product;">
        <div class="card-header">
            <h4 class="my-0 font-weight-normal">{{ product.name }}</h4>
        </div>
        <div class="card-body">
                <img src="{{ product.imagePath}}" class="img-thumbnail" alt="{{ product.name }}"> 
                <p class="lead">{{ product.msg }} {{ product.msg2 }}</p>
                <a role="button" class="btn btn-lg btn-block btn-primary" routerLink="product-details">More...</a>
        </div>
      </div>
    </div>
  </div>
</div>

Any assistance provided would be greatly appreciated. Currently, my stack includes:

Angular-cli version 6.1.0
bootstrap 4

Answer №1

To efficiently handle the logic on the index, you can utilize the following code snippet:

<div *ngFor="let item of items; let i = index">
  <div *ngIf="i % 3 === 0" class="row">
    {{ item }}
    <div *ngIf="i + 1 < items.length">{{ items[i + 1] }}</div>
    <div *ngIf="i + 2 < items.length">{{ items[i + 2] }}</div>
  </div>
</div>

Answer №2

To showcase 3 items in a row, you can leverage the power of bootstrap css classes.

Ensure your HTML file has a structure similar to this:

<div class="row">
 <div class="col-4 col-sm-4 col-md-4 col-lg-4" *ngFor="let item of products">
  {{item.name}} | {{item.imagePath}} | {{item.description}} <!-- displaying product information -->
 </div>
</div>

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

Is Clarity Design compatible with Angular 15?

After updating my project to Angular version 15, I found that the clarity version I was using was as follows: "@cds/core": "5.0.0", "@clr/angular": "5.0.0", "@clr/icons": "5.0.0", "@clr/ui&qu ...

Comparing Angular global variables: when to use static readonly in service class versus const

Which method is the better choice? @Injectable({ providedIn: 'root' }) export class MyService { static readonly VALIDITIES = new Map<number, string>([ ... ]); ... } OR: const Validities = new Map<number, string>([ .. ...

Encountering an Issue with Passing Props through Gatsby Link to Access a Prop on the

I am encountering an issue when trying to pass a value to another page for conditional rendering. The bug I'm facing is related to 'location' being undefined during the build process. Despite my efforts, I have been unable to resolve this is ...

Managing user sessions in Angular 5: Best practices and tips

Using the Spring Framework with 'Shiro' for authentication on the backend, And Angular 5 on the frontend. Calling the login API from Postman results in the same user session until the logout API is used, which is expected behavior. Postman UI ...

What is the best way to parse JSON data with Typescript?

I am dealing with JSON data structured as follows: jsonList= [ {name:'chennai', code:'maa'} {name:'delhi', code:'del'} .... .... .... {name:'salem', code:'che'} {name:'bengaluru' ...

There seems to be no clear reason as to why the Angular Service is showing

In my DataService component, I have defined two methods - one to read from a file using the cordova-file-plugin and the other to write to it. Initially, it was using the in-mem-web-api, which worked perfectly fine. However, I made some changes to switch th ...

Utilizing Glassfish Application Server and MSSQL Database for Angular Front-End Authentication in Jakarta

Embarking on my journey in the realm of web development, I am eager to implement authentication for my full-stack application. Armed with Angular 13 on the front end, Jakarta 9 running on Glassfish app server, and MSSQL database, I find myself at a loss on ...

Understanding vulnerabilities in Angular and implementing effective solutions to address them

click here to view the image Hello everyone, I encountered an error message while attempting to install Bootstrap in my project using the code npm install --saved bootstrap. Can anyone provide assistance in simpler terms? ...

Guide to updating the content of an input field

As a newcomer hobbyist, I'm attempting to automate my web browsing experience. My goal is to have the browser automatically fill in my username and password, and then click the sign-in button using a combination of JavaScript and Tampermonkey. This me ...

"Encountering a duplicate identifier export error when attempting to launch my Angular2 project using

After initiating my Angular2 Project with the npm start command, I encountered an error stating "Duplicate identifier export." Despite extensive research, I have been unable to find a precise solution to this issue. Below, you will find the package.json, t ...

Switch back emulation when moving away from page - using angular javascript

I have a unique scenario in my component.ts file on a specific page where I need to incorporate custom CSS for printing purposes: @Component({ selector: "dashboard", templateUrl: "./dashboard.component.html", styleUrls: ["./d ...

Why am I receiving a 301 status code for my API calls while using the Angular development server?

Currently, I have a .NET Core API that is being hosted on Azure. My Angular UI (version 8) is being run locally and I am trying to point API requests to my app service running in Azure. To achieve this, I have configured my Angular proxy.conf.json file as ...

Disabling behavior subjects in Angular 8: A guide to unsubscribing

Hello, I am currently working on an Angular8 application where I am utilizing Replay Subject and Behavior Subject. I have noticed that when initially clicking, the API is only hit once. However, if I switch between tabs, the subscription triggers multiple ...

I am encountering issues with the TypeScript repository build on my local machine, but it successfully passes when

I am encountering an issue with a TypeScript repository failing to build on my local machine. The error message I receive is as follows: $ tsc --pretty -p tsconfig.json ../../../../../../node_modules/@types/graphql/subscription/subscribe.d.ts:17:12 - erro ...

Should the input field only contain spaces, a validation error will be triggered by the user

I am currently working on an Angular form implementation that allows users to enter their phone numbers. I have integrated a custom directive called appPhoneExtMask for formatting the phone numbers and have also implemented Angular form validation for both ...

Introducing NextAuth: Empowering multiple users to access a single account

I've been pondering if it's possible to have multiple users for a single account provider. One idea is to create a session with a specific field indicating the active user (the one currently logged in) and then allow for easy switching between us ...

Ways to access the types of function parameters

Obtaining a method's parameter types using ReflectAPI is simple: Reflect.getMetadata('design:paramtypes', target, propertyKey); However, the challenge arises when trying to retrieve a function's parameter types as it constantly return ...

What is the proper way to add an SSL cert to an Angular HTTP API request?

Is there a need to utilize a certificate when making an API call to retrieve an access token? For example, if the method is POST at getAccess.com/getCode, how should we handle this in Postman with a certificate attached? I am currently working on an Angula ...

Creating an Angular interface that includes static values

How can we define an interface in Angular that includes constant values? For example, creating a 'State' interface that lists all the states in Australia. One way to do this is by defining the State interface like so: interface State { name: ...

Navigating with firebase authentication and angular routing

Currently, I am in the process of developing an ionic app using version 4. For this project, I have decided to utilize firestore as my database and implement firebase-authentication for user login and registration functionalities. However, I have encounter ...