Using Angular's *ngIf directive to switch between three classes

I am working with a div in Angular and I am looking to switch between 3 classes.

My HTML code looks like this:

<div *ngIf="status">Content Here</div>

In my .ts file, I have defined a variable:

status = 'closed';

The status variable can hold one of the following values...

closed, opened or hidden.

Depending on the value of status, I need to apply different classes or CSS styles to the div element.

Can anyone guide me on how to achieve this?

Answer №1

To achieve the desired outcome, make sure to utilize the ngClass directive in Angular.

<div *ngIf="status"
   [ngClass]="{'closedClass': status == 'closed',
               'openedClass': status == 'opened',
               'hiddenClass': status == 'hidden'}">Content Goes Here</div>

I trust this solution is beneficial to you :)

Answer №2

One simple method is to follow these steps:

<div [class.class1]="status == 'closed'" [class.class2]="status == 'opened'" [class.class3]="status == 'hidden'">Content Here</div>

Another option is to utilize ngClass:

<div [ngClass]="{ 'class1':status == 'closed', 'class2':status == 'opened' ... }"></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

Running Ng serve is not functioning properly with no indications of errors or messages to identify the issue

Recently, after updating to MacOS Sonoma, I encountered an issue where the ng serve command stopped working. While ng version is still functional and displays the current version, running ng serve in my project doesn't yield any results. The terminal ...

Is it possible for me to download npm locally using the command line? I am looking to install an older version of npm for a project that requires it

I currently have two Angular projects on my computer: The first project is built in Angular 14 and connected to Java. The installations of npm and ng are carried out using Maven plugins. Here is an example of the Maven plugins being called with configurat ...

Solve problems with limitations on ng2-dnd drop zones

I successfully integrated drag and drop capabilities into my Angular 4 application using the ng2-dnd library. Within my application, I have containers that can be sorted, as well as individual items within each container that can also be sorted. My goal i ...

MSW is not effective when utilized in functions within certain contexts

Trying to intercept a post request using MSW for result mocking. A handleLogin function is located within a context, but MSW cannot recognize handleLogin as a function. An error occurs: ReferenceError: handleLogin is not a function. If the API call is made ...

What is the process of integrating passportjs(node) API with the Angular cli?

After successfully configuring Node.js with PassportJS OAuth2, the need arose for Angular to call the Node API. However, they are running on different ports. While calling all of Node.js's REST APIs from Angular works fine using proxy.conf.json, an er ...

Process for generating Firebase users using Cloud Function

As I develop my Angular app with a Google Firebase backend, I encounter a many-to-many relationship between "users" and "clubs." Here's how it looks: User Club First Name Name Last Name Member Count Phone Number Collection of Documents wit ...

Creating a Connection between Angular 4 and MySQL Database using PHP for Data Posting

After spending a few weeks searching for the answer to my question, I still haven't found what I'm looking for. I understand that Angular is a front-end framework and that I have freedom to choose any database and backend for my project. Current ...

Ways to expand the nested object in an interface: A practical example using MUI theme

I've customized a Material-UI theme and I'm trying to incorporate an extra color into the palette. Here's how my initial custom theme is structured: import { ThemeOptions } from "@mui/material/styles"; export const themeOptions: ...

Can anyone explain the reason behind deserializeUser not being triggered in Angular and Express?

I have exhaustively researched all the questions that are relevant to mine on stack overflow and other platforms... Despite that, I am unable to resolve my issue... My tech stack includes Angular and Express... I am utilizing withCredentials Here is my ...

Looking for image src attribute within a string using JavaScript and appending a function to it

When using ng-bind-html in AngularJS to render an HTML string, I encountered the need to add an ng-click function to every img element. For example, here is the HTML string: $scope.html="<--some html content--> <img class='some ...

Converting JavaScript code for Angular: A step-by-step guide

I have been working on integrating a feature similar to the one demonstrated in this Angular project: https://codepen.io/vincentorback/pen/NGXjda While the code compiles without any issues in VS code, I encountered two errors when attempting to preview it ...

The dispatch function of useReducer does not get triggered within a callback function

Can anyone assist me with this issue I am facing while using React's useReducer? I'm trying to implement a search functionality for items in a list by setting up a global state with a context: Context const defaultContext = [itemsInitialState, ...

What is the best way to center text within an input field?

I've been attempting to center the text in the span tag within the input field, but using "text-align": center in my css doesn't seem to be working as expected. Interestingly, when I switched the span tag to a paragraph tag, it caused the input ...

The concealed [hidden] attribute in Angular2 triggers the display of the element after a brief delay

I am currently utilizing the [hidden] attribute within my application to conceal certain elements based on a specific condition. The situation is such that I have two divs - one for displaying results and another for showing the text "No results". Both t ...

What is the best way to configure multiple routes in a Next.js API?

Currently, I am in the process of setting up my API for my Next.js app. One issue I am facing revolves around how to properly structure my API routes. Specifically, I require 3 routes - to retrieve all accounts, create a new account, and fetch a single acc ...

Angular 2's SVG creations do not resize properly

It is a common knowledge that an svg element with the attribute viewbox should automatically scale to fit the sizes specified in the styles. For instance, let's consider a 200*200 circle placed inside a 100*100 div. Prior to that, we need to ensure t ...

Facing error TS2397: Unable to locate module '*'' when utilizing a personalized library in Angular 6/7

Currently in the process of updating my Angular project from version 2.something to the latest release. Initially, I had a custom angular.config file set up so that I could build two separate apps utilizing the same component 'library'. However, ...

Determining the Right Version of a Framework in npm: A Guide

One common issue I encounter is the uncertainty of which versions of npm, Ionic, and other tools I should have installed. It often goes like this: "Oh, there's a new version of the Ionic CLI out. Let's update." I install CLI v3.9.0. ...

What is the significance of incorporating react context, createContext, useContext, and useStore in Mobx?

In my Typescript application, I rely on Mobx for persistence and have created a singleton class called MyStore to manage the store: export class MyStore { @observable something; @observable somethingElse; } export myStore:MyStore = new MyStore(); ...

Guiding you on exporting a Typescript class with parameters in Node.js

Trying to find the Typescript equivalent of require('mytypescriptfile')(optionsObject); This is the TS code provided: export class Animal { name: string; public bark(): string { return "bark " + this.name; } constructor(color:string) ...