Does NgStyle work with a condition?

Could someone explain why this code block functions correctly:

[style.height]="events?.length === 0 ? 'calc(100vh - 105px)' : null"

while these two blocks do not work?

[ngStyle]="{'height: calc(100vh - 105px)': events?.length === 0 }"

or even this one?

[style.height]="{'calc(100vh - 105px)': events?.length === 0 }"

Answer №1

When using the ngStyle directive, remember to pass the styles as a JSON object with keys as style names and values as CSS attribute values.

[ngStyle] = "{ 'height': events?.length === 0 ? 'calc(100vh - 105px)' : null }"

In your code snippet,

[ngStyle]="{'height: calc(100vh - 105px)': events?.length === 0 }"

You have incorrectly used the full [style name]: [style value] as the key in the JSON data which will not work with ngStyle.

The correct usage of [style.height] is to set the height CSS attribute within the style HTML attribute.

For example,

[style.height]="'100px'"
has the same meaning as style="height: 100px;".

In this piece of code,

[style.height]="{'calc(100vh - 105px)': events?.length === 0 }"

You've placed a JSON object into style.height which is not valid for setting the height CSS attribute value.

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

Can $refs cause issues with interpolation?

I'm currently learning Vue.js and the course instructor mentioned that modifying the DOM element using $refs should not affect interpolation. In fact, any changes made directly to the DOM will be overridden by interpolation if it exists. However, in m ...

How can I correctly link and open a PDF file in HTML without an extension provided in the filename?

Looking for some advice. I have a client project where there are PDF files uploaded to a file structure within a LAMP Stack, but the files do not have extensions. I need the browsers to recognize that these files are PDFs and open them accordingly. Adding ...

What is the method for specifying the default option in a SELECT Tag and how can the Index of the chosen option be obtained?

Looking for HTML and JavaScript code that can manipulate SELECT and OPTION Elements. <select> <option> </option> <option> </option> </select> How do I retrieve the index of the selected option and obtai ...

Expand the width of the drop-down menu items

Having trouble with my submenu dropdown menu... I want to increase the width of my submenu without affecting the main menu, but they seem to be interconnected and I need them to function independently. .fancyNav{ /* Styles for the UL element */ ...

`Finding the appropriate place to define a variable within a React Component`

When attempting to declare the variable images within the component, I encountered an error. See the screenshot for more information: here. ...

The expression has been altered following verification. Component loading in progress

While I have come across similar questions, I haven't been able to find a solution for my specific issue. @Component({ selector: 'waiting', template: ` <div class="waiting"> <div *ngIf="isLoading" class="loader2"></ ...

Swap out a portion of HTML content with the value from an input using JavaScript

I am currently working on updating a section of the header based on user input from a text field. If a user enters their zip code, the message will dynamically change to: "GREAT NEWS! WE HAVE A LOCATION IN 12345". <h4>GREAT NEWS! WE HAVE A LOCATIO ...

The application of border-radius causes the div to extend beyond its intended height

Is there a way to achieve a smooth shadow effect on pictures without it touching the corners? I attempted to do this by adding a border-radius and box-shadow to the parent div of the images, but now the parent div is taller than the picture itself. Any sug ...

Tips for detaching jQuery from HTML

I have attempted multiple combinations without success. Below is my current code (all within the HTML document): <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="te ...

Firefox returns empty computed value in getComputedStyle function

When I use getComputedStyle on an element that has defined properties for left, right, and bottom, I noticed that Chrome returns 'auto' as the value for top, whereas Firefox returns the pixel value. However, interestingly, the top value does not ...

What is the best way to reset everything back to its original position by clicking anywhere on the screen except for the specified div?

Is there a way to make the entire screen reset to its original state with just one click anywhere on the screen, rather than having to click a specific button? Any assistance on this matter would be greatly appreciated. Thank you! JQUERY CODE $(".readNow ...

Eliminate border around image link

I can't figure out what I'm doing incorrectly... I included some CSS code a:active {text-decoration: none; border: 0px solid black} a:link {text-decoration: none; border: 0px solid black} a:visited {text-decoration: none; border: 0px solid black ...

Guide to executing specific functions based on selected dropdown options in Angular 5

Several drop down menus need to be implemented. The requirement is that when an option from the drop-down is selected, a corresponding custom function should be called. However, in the code below, what should replace opt.mtd since it is not functioning as ...

An error was encountered: The CommonModule type does not contain the 'ɵmod' property. This issue is occurring at the getNgModuleDef function in the core.js file

After transferring my project to Amazon Workspace and updating Angular to version 14.2.6, I encountered an issue where I received the error message: "Uncaught Error: Type CommonModule does not have 'ɵmod' property." This error did not occur on m ...

What is the best way to retrieve this value using javascript?

Currently, I am developing a browser extension for opera that will search for a specific element on the page when clicked. However, as a beginner in HTML and Javascript, I am completely lost and unsure of what steps to take next. The crucial value I need ...

css: discrepancy in positioning between image and header

I'm struggling to properly align an image with a header within a navigation menu. The desired outcome is for everything to be aligned at the bottom. I have set up a jsfiddle to showcase the problem: http://jsfiddle.net/graphicsxp/j2t9R/ ...

Switch up the color and eliminate the gradient on the remove button for the Qualtrics 2014 3D theme

Hello everyone, I have successfully updated the styling of the next/previous buttons in most survey themes using CSS code snippets found here. However, I am facing an issue with the newest themes ("Qualtrics 3D - Gold" & "Qualtrics 3D - 2014") as I cannot ...

What is the purpose of the asterisk symbol in CSS?

Can you explain the purpose of the star symbol in CSS and what it is commonly referred to as? I see it as a type of wild card, but I would like to research it further. #div { *zoom: 1; /*this ... * zoom : 1; display: inline; *display: inline; /*. ...

Tips for populating an input field with a variable value

Is there a way to automatically fill an input field with a specific variable value? For instance, if the number is set to 10 and a user clicks a button, can the input field be prepopulated with the number 10? var number = 10; <input type="number"> ...

Angular 2 and 4 now have a specialized node module designed to create tree-like structures within the framework

Are there any node packages available for creating tree-like structures in Angular 2 or 4, similar to what is shown here ? I am looking for the ability to customize templates within the tree. Any suggestions? ...