In Angular, use the ngFor directive to iterate through items in a collection and add a character to each item except

Currently, I am iterating through my data list and displaying it in the view using spans:

<span *ngFor="let d of myData"> {{d.name}}, </span>

As shown above, I am adding a comma ',' at the end of each item to ensure a coherent display.

This results in the following appearance:

AAA, BBB, CCC, DDD, 

However, I am facing an issue with **the last comma that I want to automatically remove.

Any suggestions on how to achieve this?

Answer №1

To implement this functionality, utilize the last local variable as shown below:

<span *ngFor="let d of myData; last as isLast"> {{d.name}} <span *ngIf="!isLast">,</span></span>

Learn more

Answer №2

To identify the final piece of data, utilize the most recent operator (assigning it to last) and showcase it only if it is not the last one.

<span  *ngFor="let dof myData; last as isLast"> {{d.name}} 
    <span *ngIf="!isFirst"> , </span>
</span>

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

Utilizing complex data with Angular 5 [Select+Option] - a comprehensive guide

I have a complex dataset stored in the app.component.ts file that looks like this: export class AppComponentimplements OnInit { tests = { 'name': 'Bob', 'grade': '5th', 'score' ...

Transform a flat 2D shape into a dynamic 3D projection using d3.js, then customize the height based on the specific value from ANG

Currently, I am utilizing d3.js version 6 to generate a 3D representation of the 2D chart shown below. Within this circle are numerous squares, each colored based on its assigned value. The intensity of the color increases with higher values. My goal is t ...

For editing values that have been dynamically inserted

In my JSON data, there is a variable named address that contains multiple objects (i.e., multiple addresses). I am displaying these multiple addresses as shown in the following image: When clicking on a specific address (e.g., addressType: Business), t ...

Utilizing Fullcalendar 5 in conjunction with Angular: Embedding Components within Events

Recently, my team made the transition from AngularJS to Angular 12. With this change, I upgraded Fullcalendar from version 3 to version 5 and started using the Angular implementation of Fullcalendar: https://fullcalendar.io/docs/angular While navigating t ...

Is there a way to focus on a specific iteration of the ngFor loop in Angular 9 using jQuery?

I'm working on a list of items inside a modal that uses *ngFor with checkboxes. The goal is to cross out the contents of an item when its checkbox is clicked. Here's the initial code using jQuery in home.component.ts: $('body').on(&apo ...

Tips for uploading files with angular and express js

Despite attempting to upload the files using middleware, I have been unsuccessful in retrieving the value from any of req.files, req.file, or req.body. ...

Is it possible for a voiceover artist to initiate API requests?

As I work on the registration feature of my application, I am faced with the requirement that email addresses must be unique in the database. Since I am responsible for the front-end development, I am considering creating a Value Object (VO) that can make ...

Connecting a pre-existing Angular 2 application to a Node.js server: A step-by-step guide

I am currently working on an Angular 2 application with the following structure : View Structure of my Angular app Furthermore, on the server side : View Structure of server app I have noticed that there are two module dependencies folders, but I am un ...

Utilizing Arrays in Typescript within the Angular Framework

I have developed a Rest API that provides data to populate two drop-down lists in a form. The information retrieved from the API is grabbed by the Angular backend and assigned to the respective drop-downs. Rather than making separate Get requests for each ...

Angular: undefined value detected during NgOnInit execution

Although the topic has been discussed, I am unable to find a solution to my specific issue. The problem lies in my parent component that returns data passed to an input. Parent component TypeScript: public productList!: IDocumentProduct[]; constructo ...

refreshing the existing component (when clicked) only if the routerlink is currently pointing to the same component

When I click on a link, I want it to take me back to the "/" route. This functionality is already working perfectly. <a routerLink="/">Login</a> However, if I am currently on the "/" route, I want the current component to be refreshed when I ...

What causes a Typescript error when attempting to escape a newline character?

My approach is quite simple: I concatenate multiple strings and format them to make them more readable. info() { return "x: " + this.xpos.toString() + "\n" \ + "y: " + this.ypos.t ...

Error: The communication channel abruptly closed before the expected response could be delivered

Before diving into the question, I want to mention that the only reference I could find related to this issue was on this Stack Overflow thread. The problem seemed to be associated with Wappalyzer, but it was reportedly fixed in version 4.0.1. Oddly enough ...

Utilize the grid system from Bootstrap to style HTML div elements

I am working on my angular application and need help with styling items in a Bootstrap grid based on the number of elements in an array. Here's what I'm looking to achieve: If there are 5 items in the array, I want to display the first two items ...

How to make a POST request with custom headers in NestJS

Has anyone successfully sent a Post request using Nestjs to a 3rd party API that needs authorization through a client-key and secret? I am looking for guidance on how to include headers in the request, ideally using axio's HttpService. ...

Show pictures stored in S3

I currently have an Amazon AWS S3 Bucket where I store images. Each object in the bucket has its own link, but when I try to open it in a browser, the image downloads instead of displaying directly on the site. This makes it challenging to view the images ...

Unable to construct a node typescript project using solely production dependencies

I am currently working on a Node TypeScript project that utilizes various third-party libraries such as express. To ensure type safety, I typically install the @types/express module as a dev dependency following common instructions. The installation works ...

Setting up grunt-ts to function seamlessly with LiveReload

I am currently experimenting with using TypeScript within a Yeoman and Grunt setup. I've been utilizing a Grunt plugin called grunt-ts to compile my TypeScript files, and while the compilation process is smooth, I'm encountering issues with live ...

Custom React component - DataGrid

Just starting out in the world of React and attempting to create a custom component with parameters. I'm curious about the correct approach for achieving this. Here's my current code snippet - how do I properly pass Columns, ajax, and datasourc ...

real-time mat-progress-bar constantly updating

Is it possible to have the progress bar updated in real-time without having to reload the page every time? <mat-progress-bar *ngIf="row.stage === 'importing_in_progress'" class="exchange-files__progress-bar" mode=&quo ...