Generating containers in Angular using *ngFor

As a beginner in Angular, Bootstrap, and front-end programming, I am looking to create multiple boxes that reveal detailed information when clicked. This is the desired outcome:

https://i.sstatic.net/Ak0eA.png

After clicking on box number 1:

https://i.sstatic.net/mXMpY.png

However, my current implementation looks like this:

https://i.sstatic.net/0R0Vx.png

I'm generating these boxes using an *ngFor loop in Angular as shown below:


<div class="container text-center" id="cryptocontainer">
  <div class="row" *ngIf="coins">
    <div *ngFor="let coin of objectKeys(coins); let i = index" id="currencybox" class="col-md-2" (click)="coinDetails(coin,i)">

      <img id="image" [src]="getImage(coin)" class="img-responsive">
      <div class="cryptoname">{{ coin }}</div>
      <div class="cryptoprice">{{ coins[coin].USD | currency:'USD':true}}</div>
      <div class="details" *ngIf="detailToggle[i]" no-lines>
        <div class="row">
          <div class="col">
            <div class="label">CHANGE(30 dni)</div>
            <canvas id="canvas{{i}}">{{ chart[i] }}</canvas>
          </div>
        </div>

        <div class="row" id="details">
          <div class="col-sm">
            <div class="label">MARKET CAP</div>
            <div class="answer">{{details.MKTCAP}}</div>
          </div>
          <div class="col-sm">
            <div class="label">CHANGE(24h)</div>
            <div class="answer">{{ details.CHANGE24HOUR }} ({{ details.CHANGEPCT24HOUR }}%)</div>
          </div>

          <div class="col-sm">
            <div class="label">MAX (24h)</div>
            <div class="answer">{{ details.HIGH24HOUR }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Answer №1

It's important to understand how to break down a UI into components when working with Angular. Moving specific details to separate components and passing data between them is key in the Angular way of development. One approach could be creating a component for the details with an input that receives the data, and another component for clickable elements that emit custom events when clicked. These components can then be used in a parent component where the state is managed.

When a clickable component is clicked, it sends its data to the parent component which updates the details property. Since the display/canvas component is bound to this property, Angular's change detection will automatically update the view.

You can find a simplified example of this communication pattern in action on Stackblitz:

Alternatively, you could create a shared service that both components use to update data when clicked. Here is the link to access the code on Stackblitz editor: Stackblitz Editor Link

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

having each individual div function on its own accord

Is there a more efficient way to make HTML and CSS function independently without duplicating the code multiple times and changing IDs or class names? Currently, only the top male and female button works when clicked. I am looking for a CSS-only solution t ...

Achieving a fixed footer at the bottom with absolute positioning on a webpage

I'm facing an issue with positioning a footer on my webpage, which is made up of div sections that are absolutely positioned. The problem arises because using the bottom property fixes the footer to the bottom of the screen rather than the bottom of t ...

Is there a way to determine the origin of a CSV file shared through a GitHub link?

I'm a beginner on Github and I need help figuring out how to trace back the origin of this CSV file: https://raw.githubusercontent.com/datasets/covid-19/master/data/countries-aggregated.csv Can someone guide me on how to locate where it was uploaded? ...

show a pie chart at the top of the div using highcharts

i am struggling with positioning the high-chart with a label, as seen in the image below https://i.sstatic.net/m9bXW.png does anyone know how to properly display the chart from the top left corner? i have attempted to adjust the settings like this { ...

Creating an SCSS class in a component that is customizable and can

After doing some research and reading various guides and blogs, I am still struggling to find the right solution for this issue. Imagine having the following classes in your main styles.scss file: .btn { padding: 5px; text-transform: uppercase; } .b ...

The MUI snackbar element lingers in the DOM even after it has been closed

Having created a global modal with the intention of only utilizing it when necessary, I've encountered an issue where the snackbar div persists in the DOM. This persistence is causing certain elements to become blocked as they appear beneath this div. ...

vaadin-grid selection issue not functioning

I'm encountering an issue with the row selection feature. The selectedItems array only updates when I select all items at once. I'm not sure if I'm missing something or if this is a bug. selectedItems: An array that contains the selected ...

Tips for continuously duplicating a value in every textbox until the final one

I am looking to implement a feature where the value of a double-clicked textbox is repeated until the end of the textbox. In my form, there are ten text boxes with different values. When I double click on the third textbox, I want the value of the third te ...

Using AJAX to submit a form and then refreshing the page within the current tab

I am facing an issue with my web page that contains multiple tabs, some of which have forms. Whenever a form is successfully submitted, the page reloads but always goes back to the default first tab. To address this, I am attempting to use a storage variab ...

Could a css style be applied to a parent element based on the status of its child element?

In my specific context, this is the HTML code I have: <div class='table'> <div> <div *ngFor='let product of this.market[selectedTab].products | orderBy:"id"' class='itemlist' [ngClass]="{' ...

Identify and target a particular DIV based on the URL for customization

My webpage has a collection of frequently asked questions (FAQs) on a page called faq.html. Here is an example structure: <div id="faq1">...</div> <div id="faq2">...</div> I am looking to create a pulsating or highlighting effect ...

What is the best way to choose content that has been clicked on?

How do I display only clicked content? Currently, I have a system where clicking a button shows the data in the nav tag. The code is written in views.py. class TopView(TemplateView): model = Data template_name = 'index.html' def get ...

What could be preventing the fill color of my SVG from changing when I hover over it?

I am currently utilizing VueJS to design an interactive map showcasing Japan. The SVG I am using is sourced from Wikipedia. My template structure is illustrated below (The crucial classes here are the prefecture and region classes): <div> <svg ...

sass: node_modules/ionic-angular/themes/ionic.variables.scss

Encountered error during the execution of Ionic Cordova build command sass: node_modules/ionic-angular/themes/ionic.functions.scss Full Error: [11:34:50] sass started ... [11:34:51] sass: node_modules/ionic-angular/themes/ionic.functions.scss, line: 35 ...

Enabling auto-expansion for textareas with every keystroke

Currently, I have implemented the following script: jQuery.each(jQuery('textarea[data-autoresize]'), function() { var offset = this.offsetHeight - this.clientHeight; var resizeTextarea = function(el) { jQuery(el).css('h ...

"Troubleshooting: IE compatibility issue with jQuery's .each and .children functions

I have created an Instagram image slider feed that works well in Chrome, Safari, Firefox, and Opera. However, it fails to function in all versions of Internet Explorer without showing any error messages. To accurately determine the height of images for th ...

A guide on implementing RxJS Observables to target a specific DIV element

Currently, I am working with Angular 2. At the moment, I have been using this method to select a specific DIV element: <div #aaa> </div> @ViewChild('aaa') private aaa: ElementRef; ngAfterViewInit() { let item = this.aaa.nativeEle ...

Creating a custom HTML table layout with both variable and fixed column widths, along with grouping headers

Is there a way to format an HTML table so that it appears like this: <- full page width -> <-20px->< dynamic ><-20px->< dynamic > +------------------+-------------------+ ¦ A ¦ B ...

Create a single datetime object in Ionic (Angular) by merging the date and time into a combined string format

I have a string in an ionic project that contains both a date and time, and I need to merge them into a single datetime object for sending it to the server const date = "Fri Jan 07 2022 13:15:40 GMT+0530 (India Standard Time)"; const time = " ...

Unveiling the Navigation Strategies Embedded in Angular Components

Currently, I have a collection of Angular components all configured with routing to assign a unique URL to each. The goal is to sequentially navigate from one component to the next based on user input. Some components may be visited multiple times at vario ...