Is it possible to create individual span elements for every value we loop through in *ngFor in Angular 8?

I need help with generating a span element for each saved tag in my collection's tag array using Firebase and *ngFor loop. Currently, I am getting one large span element with all tags separated by commas instead of individual spans for each tag. Is there a way to prevent this from happening? I have also created an interface for Saved. Thank you for any assistance.

        <div class="card">
            <div class="card-body">
                <h5 class="card-title text-center">{{saved?.title}}</h5>
                <hr />
                <div *ngFor="let tag of saved.tags">
                    <span class="badge badge-pill badge-primary">{{tag}}</span>
                </div>
                <hr /> <a href="#" class="btn btn-primary  offset-4 col-md-4">View</a>

            </div>
        </div>

//Saved interface in Saved.ts file
export interface Saved {
  id: string;
  title: string;
  tags: string[];
}

Answer №1

To ensure the span element is repeated instead of the div, adjust your code accordingly. Additionally, be sure to reference the individual tag rather than the array inside.

If the tag includes a name or title attribute, replace {{ tag }} with {{ tag.title}}

Upon examining the interface, it appears that only {{ tag }} is required.

<div>
  <span *ngFor="let tag of saved.tags" class="badge badge-pill badge-primary">
    {{tag}}
  </span>
</div>

Check out the Angular documentation for utilizing *ngFor in data display.

Answer №2

Currently, you are accessing the array within your *ngFor. This means that you will display the entire list of n tags for n iterations. To address this, switch from {{saved?.tags}} to {{tag}}. By doing so, each tag will be displayed within its own div alongside a span element.

To achieve one span per tag, modify your code as shown below:


    <div class="card">
        <div class="card-body">
            <h5 class="card-title text-center">{{saved?.title}}</h5>
            <hr />
            <div>
                <span class="badge badge-pill badge-primary" *ngFor="let tag of saved.tags">
                    {{tag}}
                </span>
            </div>
             <hr />
            <a href="#" class="btn btn-primary  offset-4 col-md-4">View</a>
        </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

Error message TS2339 in Typescript: The property '__super__' is not found on the type '($element: any, options: any) => any'

Having trouble with Javascript code inside typescript. $.fn.select2.amd.require([ 'select2/data/array', 'select2/utils' ], function (ArrayData, Utils) { /* tslint:disable */ function CustomData ($element, opti ...

Unable to establish a connection with the server data in pusher.js

I have been trying to set up a collaborative drawing application by following this tutorial. Although I have managed to run the application, the collaborative drawing feature is not working as expected. I have correctly configured the PUSHER_APP_ID, PUSHER ...

Error encountered when deploying a React app on Heroku: "Uncaught SyntaxError: Unexpected token <"

After deploying my Heroku app with the create-react-app buildpack, I noticed a strange issue. When trying to access my domain for the first time, the page loads as white and the developer console shows an error: Uncaught SyntaxError: Unexpected token < ...

Verify if the page was generated as a regular Page or a modal page

Within the UpgradePage, I have a scenario where I want to navigate to the same page either through the side menu or as a modal page using push/setRoot. Q: The method upgradeLater() is where I need to make a decision on whether to redirect to another page, ...

Angular encountered an error when trying to access the property "fruits" of an undefined object

When working with Angular, I encountered an issue where I received the error message "cannot read property 'fruits' of undefined." Within my class definition, I have included the following: export class MyClass implements OnInit { fruits: any[] ...

Loop through each object within an array and verify if a specific property containing an array matches a given value in JavaScript

I am looking for a way to iterate over objects within an array that contains another array called userHandle. I need to loop through the userHandle array and check if any of the values match a string I define as uid. Can anyone provide me with Javascript ...

Implement Angular and RxJS functions sequentially

this.functionalityClient.activateFeature(featureName) .pipe( concatMap( feature => { this.feature = feature; return this.functionalityClient.setStatus(this.feature.id, 'activated'); } ), con ...

Tips for integrating 2 way data binding into model-driven forms

When working with Angular 2, one approach to creating forms is the model-driven method. This means that controls may lose their two-way data binding, unlike in the template-driven approach with ngModel. What is the best way to combine two-way data binding ...

Implementing JavaScript logic to proceed to the following array within a 3D array once a specific condition is met

I'm currently tackling a challenge that requires me to obtain a specific number using a given 3D array. This array consists of 2D arrays, each containing the same number repeated x times. The parent array is sorted from largest to smallest. Here&apos ...

Using Angular 2+, learn how to effectively utilize and monitor the output emitter when utilizing the createComponent factory method

When I dynamically create a component in ParentComp, the code looks like this: const factory = this.resolver.resolveComponentFactory<ChildComp>(component); this.componentRef = this.container.createComponent(factory); this.componentRef.instance.dataC ...

Ways to incorporate horizontal scrolling in mat autocomplete

I have an Angular app with auto complete functionality. I am trying to add horizontal scroll to the mat-option by applying CSS styles, but it's not working as expected. .cdk-overlay-pane { overflow-x: auto; } I also tried following the instruc ...

Bypassing a digit in the Java series

There's something very intriguing about this, I've noticed. But before I delve deeper into it, let me first show you the code so you can grasp what I'm talking about. Here's the snippet of code: public class Qn3 { static BigDecima ...

Seeking direction on how to utilize a switch statement with an array in C++. Any suggestions?

My goal here is to work with 3 students, assigning 3 grades to each of them and then calculating the average of those grades along with assigning a letter grade. This part is functioning well so far. Now, I need to calculate the class grade average by addi ...

Issue with PrimeNG overlaypanel displaying error message "Functionality of this.engine.setProperty is not available"

After importing OverlayPanelModule as @NgModule in parent.module.ts, I added the following code in child.component.html: <p-overlayPanel [dismissable]="false" #overlay> Content </p-overlayPanel> However, upon testing, I encountered the error ...

What are the benefits of using default ES module properties for exporting/importing compared to named module properties?

Currently studying the Material UI documentation, I came across this statement: It is noted in the example above that we used: import RaisedButton from 'material-ui/RaisedButton'; instead of import {RaisedButton} from 'material-ui&apo ...

Showing identification and name within a URL path - Angular

I am looking to update a URL path within a website from http://localhost:4200/viewbrand/1 to http://localhost:4200/viewbrand/1/soap, where "soap" is added after the ID in the path. The current code in the routing module.ts file is as follows: { path: &apo ...

Iterate over an array in PHP, extract JSON data from files with corresponding names, and then store the extracted JSON as properties

Apologies for the intricate title - describing my situation is quite challenging. This is JSON file Egg.json: {"1":{"name":"Egg"}} This is index.php: error_reporting(E_ALL); class Test { public $arrEgg; public $arrTypes = array("Egg"); pu ...

Creating a three-dimensional array in C++

What is the correct way to initialize a 3D array in C++? int array[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; ...

Having difficulties injecting a Service into a TypeScript Controller

I recently started working with TypeScript and I created a controller where I need to inject a service in order to use its methods. However, I am facing an issue where I am unable to access the service functions and encountering an error. Error TypeError ...

Find the calculated values within an Angular Material table

I came across this fantastic example of an Angular Material table with checkboxes that fits perfectly with what I want to implement in my application. However, I am facing a challenge - I need to calculate the total value of the checked rows. Specifically, ...