How can I assign a specific class to certain elements within an *ngFor loop in Angular?

I have a situation where I am utilizing the *ngFor directive to display table data with the help of *ngFor="let record of records". In this scenario, I am looking to assign a custom CSS class to the 'record' based on specific conditions; for example, when 'record.name' is equal to 'something'. Can anyone advise if achieving this customization is feasible?

Answer №1

A solution to this could be utilizing [ngClass] in the following way:

<div [ngClass]="{'record': record.name === 'something' }" *ngFor="let record of records">

Answer №2

Here’s an alternative approach you can experiment with:

<ul class="r " *ngFor="let place of places">
  <li [ngClass]="place.id === variable ? CSSClass1 : CSSClass2" >
   {{AnotherThing.id}} 
  </li>
</ul>

Answer №3

<div *ngFor="let item of items">    
  <p [ngClass]="item.type === 'special' ? 'specialStyle' : 'regularStyle' ">{{item.name}}</p> 
</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

What is the best way to shorten text in Angular?

I am looking to display smaller text on my website. I have considered creating a custom pipe to truncate strings, but in my situation it's not applicable. Here's what I'm dealing with: <p [innerHTML]="aboutUs"></p> Due to t ...

Ensure that the variable is not 'undefined' and that it is a single, clean value

In my node backend project, I've encountered a situation with my TypeScript code where a variable occasionally prints as the string 'undefined' instead of just being undefined. Is there a more elegant way to check that the variable is not eq ...

What is behind the inconsistency of RxJS versions?

Trying to set up a node/angular2 solution on cloud9 has been quite the challenge. Below is my package.json: { "name": "example", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www", "postinstall": "typings install", ...

Does anyone know of a convenient tool that can automatically provide suggested street names for mailing addresses in every country across the globe, for free?

Is there a convenient tool that automatically completes street addresses for various countries worldwide? I'm considering options similar to YQL from Yahoo or Foursquare. I'd like to provide users with suggestions of known street names as they ...

Select an item, then toggle classes on the other items to add or remove them

My code includes the following HTML elements: <ul> <li class="one"><a href="#">one</a></li> <li class="two"><a href="#">two</a></li> <li class="three"><a href="#">three</a>&l ...

When trying to serialize elements from a form loaded by Firefox using AJAX, encountering difficulties due to

Visit the live version of this at . When prompted for a formId, input BroadRunTrack2013. Follow by adding an item to cart and entering player name information (you can use random data for now). Select the Runner's Hat, choose color/size, specify quant ...

Is the fixed header see-through?

I have a query regarding my website design. Currently, the fixed header on my site is transparent and when I scroll down, the content overlaps the header. However, I want the header to remain above the content. Can anyone provide assistance with this issue ...

How can we bring in prop array values in React?

I've been working on developing a small music player program in React. Is there a way to import all these values together with a single import statement? I noticed that manually inputting the values into the props array doesn't work because the ...

What is the best way to declare module variables in a Node.js environment?

When it comes to declaring variables when requiring modules in nodejs, there are different styles followed by well-known developers. For instance, TJ Holowaychuk uses a style like this: (method1) var connect = require('connect') , Router = req ...

How to effectively utilize API references in TypeScript within Angular 7

Greetings, I am new to working with AngularJS and I am currently attempting to customize the appearance of a particular library. If you are interested, here is the link to the library: https://github.com/sanjayV/ng-image-slider (API references can be foun ...

Sending JSON data two times to the ASP.NET MVC controller via POST request

function onTestComplete(content) { var url = '<%= Url.Action("JsonTest","Organization") %>'; $.post(url, null, function(data) { alert(data["name"]); alert(data["ee"]); }); } <% using (Ajax.BeginForm("JsonTe ...

Adjusting a NodeJS module for easier integration into codebases

I have organized some functions in a file within a node module structure... Here are the files and folder structure I created: - package.json - README.md - LICENSE.md - code |_______ code.js Currently, to use these functions, I include them like th ...

Maintain division contents while navigating

I need a solution to keep my div (NewCall) open even when the user navigates between pages in my Single Page Application. The NewCall div is located in my layout page and is triggered by user click. However, whenever I navigate to another page, the div cl ...

The try-catch statement in Typescript is generating an inconsistent return error

I've encountered an issue with a TypeScript function that is flagging inconsistent return error. The function includes a try block that returns a value and a catch block that throws an error, resulting in the inconsistency. I am struggling to find a w ...

Deactivate zoom on the viewport and display the entire page

Hello, I am currently working on my website www.ecogo.io and I am trying to get the entire page to display instead of just a portion. However, I am encountering an issue where the page loads zoomed in on mobile browsers like Chrome for Android, whether Des ...

Is it time to ditch daylight saving time - am I managing my dates and times correctly?

Currently, I am in the process of working on a PHP/mySQL web application where dates are stored as unix timestamps in columns that are UNSIGNED INT(10). When it comes to displaying these dates in the web view, we utilize moment.js to parse the numbers. On ...

Is there a workaround for the React useContext issue in Typescript aside from using <Partial>?

I am currently working on a React app that utilizes the useContext hook, but I am facing challenges with correctly typing my context. Here is the code snippet in question: import React, { useState, createContext } from 'react'; import endpoints f ...

Combining Two DIVS Side by Side

Hey there, I am working on a timeline using two divs within a table cell. My goal is to have these divs overlap instead of appearing one below the other. The position attribute for all the DIVs inside the cell must remain unchanged due to the drag/drop fu ...

Setting up Angular 2 for Highcharts: A Comprehensive Guide

Having trouble implementing a highchart component in my Angular 2 project after setting it up with the latest version. I've created a linechart component directory and tried to add the chart using the <line-chart></line-chart> tag in the H ...

Updating model/schema dynamically within Express Router

My express server handles api calls by directing them to specific routes. app.use('/api/data01', require('./routes/dataRoute01')) app.use('/api/data02', require('./routes/dataRoute02')) app.use('/api/data03&apo ...