Display different icons in an Angular application based on the value received from an API in real

My goal was to create a dynamic form that displays icons for the fields I have created. Here is a snapshot of my UI screen showing the field explorer with the list coming from an API.

https://i.sstatic.net/4Ye9G.png

I need to place an icon in front of each text in the field explorer.

Below is my current HTML code:

       <div class="fieldexplorer">
           <div class="sectionHeader_field">
                Field Explorer
           </div>
           <div *ngFor="let label of labels;">
                  {{label.labelName}}
           </div>
       </div>

This is my TypeScript code snippet:

    /*Display text from backend for field explorer*/
    this.labels = this.fieldData[0].fields.map(element => {
      const data = {
        labelName: element.labelName,
        fieldType: element.fieldType
      };
      console.log(this.data);
       return data; 
       
    });

For more detailed TypeScript code, please see the full-length answer below.

Here is a CSS snippet for displaying icons based on field type:

  .number::before{
        font-family: "Font Awesome 6 Free";
        content: "\f893";
        display: inline-block;
        padding-right: 3px;
        vertical-align: middle;
        font-weight:900;
    }
    ...

The goal is to show relevant icons based on the field type. If it's text, show a text (T) icon; if it's a radio button, show a radio icon, and so on.

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

I'm unsure about where to implement the array logic within this code.

Answer №1

To streamline the process, I suggest creating an object containing all possible icons. For instance:

iconSet = {
  Number: "Number icon placeholder",
  Text: "Text icon placeholder",
  Radio: "Radio icon placeholder"
}

If your array resembles the following:

data = [
  {name: "fieldName1", type: "Number"},
  {name: "fieldName2", type: "Text"},
  {name: "fieldName3", type: "Radio"},
]

During template iteration, you can implement the following:

<div *ngFor="let item of data">
  <p>{{iconSet[item.type]}}</p>
</div>

The above code is a sample demonstration and should be tailored to fit your specific needs.

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 trouble locating modules or properties with ANTLR4 TypeScript target?

Having reached a frustrating impasse, I am seeking assistance with a perplexing issue. My attempt to integrate TypeScript with ANTLR4 has hit a snag, and despite exhaustive efforts, I am unable to pinpoint the root cause (with limited documentation availab ...

Easily transferring sessionStorage values between components in Angular

I am looking for assistance on storing a user ID in sessionStorage and retrieving it in a blog component. Can anyone guide me on how to achieve this? LoginComponent.ts import { Component, OnInit } from '@angular/core'; import { Router, Activate ...

When using React MUI Autocomplete, make sure to handle the error that occurs when trying to filter options using the

I am trying to implement an autocomplete search bar that makes a custom call to the backend to search through a list of tickers. <Autocomplete multiple id="checkboxes-tags-demo" options={watchlis ...

Struggle with Transmitting Information in Ionic 2

I have been working on an Ionic project that involves a JSON file with preloaded data. The initial page displays a list from which a user can select an item to view its content. However, I am encountering a "Response with status: 404 Not Found for URL" err ...

TypeScript is unable to recognize files with the extension *.vue

Can someone assist me with an issue I'm facing in Vue where it's not detecting my Single File Components? Error message: ERROR in ./src/App.vue (./node_modules/ts-loader!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/Ap ...

Updating the selected state of an Angular 2 material mat-chip

Attempting to update the selected property of a chip named "chip" results in an ExpressionChangedAfterItHasBeenCheckedError when changing the binding property. HTML <mat-chip-list> <mat-chip *ngFor="let w of weekDays" [selectable]="tru ...

Enter data into a form control to update the browser URL

I have a table in my Angular application with search boxes above the header. When users enter values into these search boxes, the corresponding column in the table gets filtered. My goal is to update the URL directly with the values entered in the search ...

Inference of generic types within a TypeScript generic

In my coding journey, I came across a situation where I was dealing with generic classes. Specifically, I had a Generic class Generic<T> and another one called GenericWrap that used Generic as its maximum type parameter (denoted as U extends Generic& ...

Mono repo project utilizing Angular 4+ and Typescript, enhanced with Bootstrap styling

Looking for a project to practice with Angular 4+ using Typescript and a Bootstrap template. Hoping for a setup where I can just run npm install and ng serve to start. Any recommendations for mono repos would be highly valued! ...

Utilize the multipart/form-data approach for sending files with Angular 6

I attempted to send two fields to a backend service. One field is a typical string, while the other is a file field. However, when I utilize the post method of the Http Client, I encounter a 500 Error from the server notifying me that the content-type is n ...

Incorporating Classname into series data in Angular Highcharts

My current challenge involves applying css class names to the data in a pie chart. I am working on implementing a pie chart with customized colors using Angular Highcharts. I discovered that when using the regular version of Highcharts, the property &apos ...

Defined a data type using Typescript, however, the underlying Javascript code is operating with an incorrect data type

Recently delving into Typescript and following along with an educational video. Encountered a strange behavior that seems like a bug. For instance: const json = '{"x": 10, "y":10}'; const coordinates: { x: number; y: number } = JSON.parse(json); ...

Having trouble with AWS, Angular, and Docker integration issues

I have successfully dockerized my Angular app using the following Dockerfile: FROM node:8.9 as node WORKDIR /app COPY package.json /app/ RUN npm install COPY ./ /app/ ARG env=prod RUN npm run build -- --prod --environment $env FROM nginx:1.13 COPY -- ...

Tracking button clicks on Angular Material using Google Analytics through Google Tag Manager

I'm currently utilizing the Universal Analytics tag within Google Tag Manager to monitor user interactions. I'm looking to establish click listeners in GTM that will trigger when specific buttons on the page are clicked. These buttons are Angular ...

Is it possible to utilize enums as keys in a Json structure?

I am currently utilizing TypeScript in conjunction with Node.js (MEAN stack). My aim is to incorporate an enum within the property/schema of a JSON object. An example of the enum would be: enum KeyEnums { A: "featureA", B: "featureB&qu ...

Trigger the React useEffect only when the inputed string matches the previous one

Currently, I am in the process of creating my own custom useFetch hook. export const useFetch = <T extends unknown>( url: string, options?: RequestInit ) => { const [loading, setLoading] = useState(false); const [error, setError] = ...

Typescript types for React Native's SectionList: A comprehensive guide

Currently, I am in the process of developing a React Native app using TypeScript. In order to display information in a structured manner, I decided to implement a SectionList. Following the official documentation, I have written the following code snippet: ...

Problem with Typescript compilation in lerna package

Presently, my project is structured with lerna/react/TS setup as shown below: . ├── lerna.json ├── package.json ├── packages │ ├── patient │ │ ├── package.json │ │ ├── src │ │ │ └── ...

Creating seamless compatibility between the elliptic library in JavaScript and the ecdsa library in Golang for efficient cross-platform operations

I am having issues with validating a signature created using the elliptic JavaScript library and the ecdsa library from Golang. The elliptic curve in question is secp256k1. Below are some snippets of code: Here are the TypeScript utility functions: impor ...

Error: It is not possible to add the "providers" property as the object is not extendable within N

Ever since updating to the latest version of NGRX, I've been encountering an issue: users$= createEffect(() => this.actions$ .pipe( ofType(users.LOAD_USERS), tap((action: users.LoadUsersAction) => { action.item.name = ...