Pass the condition as a variable to the class name in Angular

I need to apply a conditional class to a div. The issue I am facing is that I want the "myClassCondition" to be a string as shown below. When I use

<div [class]="7 > 6 ? 'bg-red' : null"> MyText </div>
, it works perfectly. However, I want to pass it from a .ts file as a variable like this. How can I achieve this?

<div [class]="myClassCondition"> MyText </div>

-

myClassCondition = "7 > 6 ? 'bg-red' : null";

Answer №1

If you want to experiment with styling, consider the following:

.component.html

<div [class.bg-green]="myCondition"> SomeText </div>

.component.ts

myCondition = 10 > 5 ? true : false;

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

Exploring the power of Node JS with Promise.all and forEach within a nested array

When working on a Node app with typescript, I encountered the need to iterate through an array while calling an asynchronous function inside the loop to fetch information about related items for each item in the array. The function is called for each relat ...

Can you identify the specific syntax for a 'set' function in TypeScript?

I have a TypeScript function that looks like this: set parameter(value: string) { this._paremeter = value; } It works perfectly fine. For the sake of completeness, I tried to add a type that specifies this function does not return anything. I experimen ...

Group data by date in a hashmap

In my Spring Boot backend, I am managing apps and games with a Rating Object that saves rating history in a HashMap. // Number of history by day @ElementCollection(fetch = FetchType.EAGER) private Map<String, Integer> ratingHistory = new HashMap<& ...

Error: Unused variable: '_' has been declared but not utilized

While working on my Next.JS project with TypeScript, I encountered an issue when trying to run the build. The error message stated: Type error: '_' is declared but its value is never read. I attempted to resolve this by setting "noUnusedLocals" ...

Encountering difficulties when trying to deploy Angular 4 with TypeScript on Heroku

I have been facing an issue while trying to deploy my angular 4 app on Heroku. The deployment fails to proceed properly even though everything works fine locally. Here are the relevant files: package.json { "name": "webcli2", "version": "0.0.0", " ...

Angular: Simplifying nested array filtering

I'm trying to customize my sidebar menu by hiding certain paths based on the user's role. Here are some example routes: export const ROUTES: RouteInfo[] = [ { path: '/overview', title: 'Overview', type: 'li ...

The expression '() => boolean' cannot be assigned to type 'ReactNode'

Could someone please assist me in troubleshooting this TypeScript error? Type '() => boolean' is not assignable to type 'ReactNode'.ts(2322) Here's a snippet of my code for reference: export const Palindrome:React.FC<{}> ...

What are the steps to connecting incoming data to an Angular view utilizing a reactive form?

Hello, I am currently fetching data from an API and have successfully displayed the teacher values. However, I am unsure of how to utilize the incoming array values for "COURSES" in my Angular view. This is the response from the REST API: { "courses ...

Even after importing the API key, I am still receiving the message "This domain is not registered with Tiny Cloud."

I have implemented the TinyMCE code provided in their documentation: <editor [init]="{ apikey: '<secret>', height: 500, plugins: [ 'a11ychecker','advlist','advcode','advtable',&apos ...

The process of loading images along with a brief list of users in the ngOnInit function of Angular encounters failure as the images consistently return

I am struggling with loading images of students from the server to display alongside their details on my page, which contains a few bootstrap cards. Even though I have tried implementing a solution using TypeScript code, I am unable to fetch the images suc ...

`Incredible sequence of events unfolding`

In order to add an additional action after the deletion process is complete, I must make modifications to an epic. The current epic code is as follows: const deleteComponentEpic: Epic< AppActions, AppActions, AppStore, ComponentDetailsEpicsDepen ...

Changing Variables in Angular 5 Using Shared Data

Within ComponentA, there is a variable that takes its value from a variable within a shared data class. Here is what ComponentA looks like: import { SharedData } from '../../shared/shared.data'; ... export class ComponentA implements OnInit { ...

Receiving an error message "Cannot read property 'X' of undefined" when invoking a sibling method

I'm completely new to Angular and trying to understand how events work within this framework. Here is my Parent Template setup: <child1 (myEvent)="child2.testMethod()"></child1> <child2 #child2 *ngIf="show"></child2> When I ...

Creating a contravariant function member in TypeScript?

I am facing a challenge with a class that contains a member which is a function taking an instance of the same class: class Super { public member: (x: Super) => void = function(){} use() {const f = this.member; f(this)} } However, I need the me ...

Error: The request was not successful in creating and populating a list of type Microsoft.AspNetCore.Http.IFormFileCollection

I am encountering a Bad request error when trying to upload a file, convert it into JSON, and pass it to my .NET Core WebAPI controller. Below is an error screenshot that I received along with logging the model. https://i.sstatic.net/grSf4.png Here is m ...

Angular2 - Retrieve data in XLS format from RestService并下载

I am looking to create a web service using Apache POI to generate an Excel file (xls, xlsx). Below is the snippet of code I have put together: @RequestMapping(value = "/export", method = RequestMethod.GET) public void exportXlsx(HttpServletResponse respon ...

Is it Observable or Not? Typescript Typehint helping you determine

I'm facing an issue while writing a function with correct typehints. It seems to work fine with explicit type hinting but breaks down when trying to use automatic type hinting. Can someone please help me identify the error in my code and suggest how I ...

Methods for assigning values to a formControl using an array

I have an array of objects and I am attempting to loop through the array, dynamically setting values to a formControl and not displaying anything if the value is null. I have searched for similar solutions but haven't found any references or examples ...

Using Angular 6 to Share Data Among Components through Services

I am facing an issue in my home component, which is a child of the Dashboard component. The object connectedUser injected in layoutService appears to be undefined in the home component (home userID & home connectedUser in home component logs); Is there ...

Stop receiving updates from an observable once you navigate away from an Onsen UI page

I am facing an issue with my Angular 2+ app that utilizes Onsen UI. I have set up some components as pages and I am using the ons-navigator to switch between them. The problem arises when I subscribe to an observable in an ons-page and the user navigates ...