How to Combine Various Conditions in a ngStyle Directive?

I am working with a NgStyle Tag and would like to incorporate multiple conditions, but I am encountering issues where it doesn't work or produces errors.

Here is the code snippet that I have been experimenting with:

 <div class = "card" [ngStyle]="{'height': Card.duration.Hours == 1 ? '130px': height}"
                      [ngStyle]="{'height': Card.duration.Hours < 1 ? '100px': height}"
  >

Currently, only the first condition seems to be effective. When I attempt to combine these conditions, error messages are displayed. What is the best approach for implementing multiple conditions?

Answer №1

It is not advisable to use multiple instances of [ngStyle] within the same div element. It is recommended to utilize [ngClass] instead. Here is an example for reference:

HTML

<div  [ngClass]="{ 'height100': hours === 1, 'height200': hours === 2, 'height300': hours === 3}">
  test
</div>

TS

export class AppComponent  {

  hours: number = 3;

  constructor () {

  }
}

CSS

.height100 {
  height: 100px;
  background: red;
}

.height200 {
  height: 200px;
  background: yellow;
}

.height300 {
  height: 300px;
  background: green;
}

Check out the StackBlitz Demo here

Answer №2

Give this a shot

[ngStyle]="{'height': ((Card.duration.Hours == 1) ? '130px' : height)}"

Answer №3

Is this code functional?

[ngStyle]="{'height': Card.duration.Hours == 1 ? '130px': ( Card.duration.Hours < 1 ? 100px : height)}"

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

Issues encountered when attempting to insert columns while ensuring the footer stays at the

Every time I attempt to include columns on my webpage, my footer becomes unstuck and the content spills into the footer area. However, everything works perfectly fine when I directly add content to the holding content div. Sample HTML code: <div ...

Preserving content following the use of jQuery's fadeIn/fadeOut methods

I have a jQuery function that fades in and out a sprite (sprite without text) from one background to another, and it's working as intended. However, this function is also fading out the text within an element. HTML: <div id="menu"> <ul c ...

How to Adjust CSS Animation Background to Fully Cover Responsive Height

Hi there! I've been working on adding some CSS code to my ReadyMag project, but I'm having trouble with odd aspect ratio browser windows. The code I have is almost doing what I want, but it's not covering the full width and height. How can I ...

Ways to ensure the bootstrap table header width aligns perfectly with the body width

I am having an issue with my bootstrap table where the header width is smaller than the body width because I set the table width to auto. How can I align the header and body widths? Here is a link to a plunker showcasing the problem. https://plnkr.co/edit ...

NgFor is failing to display data from a fully populated array of objects

CLICK HERE FOR THE IMAGE So, let's tackle the issue at hand. I have an array that contains data fetched from an API. These data points are essentially messages. Now, below is the TypeScript file for a component where I aim to display all these messag ...

Expanding the rowspan within a table column has the effect of reducing its overall width

I have created a table with two rows, where the first row has three columns and the second row has two columns. The middle column in the first row has been set to rowspan="2". However, the issue is that it appears smaller than its intended width. .kolon ...

If the parent class in HTML has a class of "active," then use jQuery to

I have attempted this, but unfortunately did not achieve the desired result. I am just brainstorming my ideas here. HTML <html> <div> <div class="spinner"> </div> </div> </html> Jquery if (".spinner"){ th ...

How to center a div with a overlay in CSS?

I have created an overlay with a div on top of it, and I am currently struggling to center the div over the overlay. I have tried adjusting the left and right properties, but nothing seems to be working. .request-estimate { position: absolute; ...

Troubleshooting: Border not showing up in Tailwind CSS (React JS)

Below is the code snippet from my JSX file: import { useState, useEffect } from "react"; import { PropTypes } from "prop-types"; import { HashtagIcon } from "@heroicons/react/24/outline"; // Function to fetch categories from the API (temporary replaced wi ...

Page loading causing sluggishness in Angular application

I've been encountering this problem for quite some time now and have searched extensively online for solutions. However, I believe I may not be using the correct terminology to accurately pinpoint the issue. Within my package.json, I have included th ...

Having trouble setting the InnerHTML property of Null on my Ionic app, what could be the issue?

I'm working on a code to display the remaining time for generating a random code in the DOM. var count = setInterval(function () { var date = new Date(); var currentSecond = date.getSeconds(); ...

Discover how AngularFire Storage can efficiently handle a list of items by using the listAll method to create

Seeking assistance in creating an array of promises and using RXJS forkjoin to execute them all for building a result set. Currently utilizing AngularFire's storage module to retrieve all uploaded files from a specified location: getFileList(folder ...

Does the height of the rendered font adjust based on the length of words in Japanese formatting?

I can't figure out why this strange issue is occurring... It seems that the font "size" (or rather, the height it occupies) changes depending on the length of a word. I tried to reproduce this in English without success, but it consistently happens w ...

Leveraging .col-* within a .d-flex container in Bootstrap 4

What is the recommended practice for using .col-* as children in a .d-flex container? Should .d-flex be used with .row instead? I have noticed that the Bootstrap 4 Docs do not show any examples of using a .col-* in a .d-flex container. The layouts provide ...

Securing your application using Google authentication in Angular(17) and ASP.NET Core 8 Web API

In my current project, I am utilizing Angular 17 for the frontend and ASP.NET Core 8 Web API for the backend. The next step in this development is to integrate Google authentication so users can sign in using their Google accounts. Despite my research effo ...

CSS text width going from left to right

I am attempting to create a text fade effect from left to right, inspired by the technique discussed in this answer. However, I would like the text to be concealed behind a transparent background div instead of the white background used in the example. I ...

How can you animate the background of a website using AngularJS - CSS or JavaScript?

My aim is to create a dynamic animation for the background image when the view changes. The current background image is set through a function defined within MainController: // app/js/controllers.js $scope.getBg = function() { return $route.current.sco ...

Angular-cli and Chrome extension compatibility causing unexpected UI problems post-build

I developed an application with a sticky header at the top using angular2 mdl layouts. Recently, I made changes to the code to create a more customizable header. However, after building the app (ng build) and importing it as an unpacked extension, the con ...

Can CSS calc() achieve modulus behavior?

Is it possible to dynamically adjust the height of an element based on screen size using calc(), while still maintaining alignment with a specified baseline grid? The height should always be a multiple of the defined variable $baseline. I've noticed ...

Tips for avoiding a noticeable sliding drawer when changing the direction of an HTML element

I am encountering an issue with the Daisy UI drawer component on my page. When I attempt to change the page direction using a JavaScript function, the drawer animates visibly from left to right or right to left, which is not the desired behavior. I have tr ...