Using a hyphen when field values are empty

I've been working on a project using Angular. The code snippet in my HTML file looks like this:

      <div>{{ data.date | date:'d-MMM-y' || '-' }}</div>
       <div>{{ data.id  || '-' }}</div> 

The issue I'm encountering is that when the value from the API for fields like date or id is null, the next div element shifts upwards. What I want is for the div to remain in its original position even if the value is null. To achieve this, I added || '-' to all the fields in my code, but it doesn't seem to be working as intended. The dash "-" should appear in the HTML when the API value is null, but it's not showing up. Can anyone guide me on how to fix this?

Answer №1

Almost there! Just a small tweak with the parentheses:

<div>{{ (data?.date) ? (data.date | date:'d-MMM-y') : '-' }}</div>

If you want to see it in action, check out this Stackblitz: https://stackblitz.com/edit/angular-vb4peg?file=src/app/app.component.ts

Simply add a comment to the line

this.data["date"] = new Date();
and observe the difference.

Answer №2

To tackle this scenario, you have the option to utilize the nullish coalescing operator.

Here is how it can be implemented:

<div> {{ (data.date | date:'d-MMM-y') ?? '-' }} </div>
  <div> {{ data.id ?? '-' }} </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

Is there a method in Bootstrap 4 to create a table row that can be clicked on?

Is there a way to make the rows in a table clickable when using the 'table-hover' class in B4? <table class="table table-hover"> <thead> <tr> <th>#</th> <th>First Name</th> <th ...

Guide to automatically changing a background image according to different routes within a React framework

I'm facing an issue with updating the background image dynamically based on different routing options, as no background is being displayed. I've attempted using inline styling and verifying the correct directories are referenced. Initially, the ...

Apply this CSS style using JQuery

Here is the CSS I am currently using: #hideSidebarText:hover > #hideSidebarArrows { background-position: -282px -51px; } I want to change the "background-position" property using JQuery. I attempted the code below, but it didn't work as intended: ...

Is there a way to automatically assign a default value to radio buttons when the page is loaded?

Working with OpenERP 7, I encountered a problem with two radio buttons that should change the values of a table. After creating an event that triggers a Python function to execute an SQL query, I found that upon reloading the page, the radio buttons revert ...

Bootstrap gallery with images that resize proportionally

Attempting to create something simple, but currently unsure how to proceed. A few months ago, I had a concept in mind, but unfortunately lost the files and now feeling stuck. Using Bootstrap to construct a gallery with two different image sizes: 1920x1200 ...

The Angular Library seems to be malfunctioning as it does not execute the ngOnInit

I've been following the instructions from Angular CLI 6 to create a library, which can be found here. So far, I've successfully created and built my library. It includes a Component that I'm using for the UI and has an HTML selector. @Compo ...

Tips for utilizing javascript to reset the heightline of the preceding keyword during a keyword search

Using JavaScript, I have implemented a search keyword function that highlights specific words in a paragraph. However, I am facing an issue. Currently, when searching for the next keyword, the previously highlighted text remains in red instead of reverting ...

Error: Unhandled Promise rejection in Angular 2 with C# API - ReferenceError: _body variable is not defined

I am currently working with a C# API that returns data in the following format: status: 200, ok: true, statusText: "OK", url: "http://localhost:53619/api/EventList/All" _body: "[ { "id":"6 ...

The navigation for both the Bootstrap 4 Card Slider and Half Slider is identical

I've noticed an issue with my Card slider and half slider. Whenever I navigate through the Card slider, the half slider also moves along with it. However, this is not the case when navigating the half slider; the Card slider remains stationary. It see ...

Is there a way to use JavaScript to determine our current position in a CSS Keyframe animation based on a percentage?

After some thought, I realized that it's possible to detect when a CSS animation starts, finishes, or repeats by using the animationstart, animationiteration, and animationend events. For example: document.getElementById('identifier') ...

Struggling to style a nested table using CSS

Please take a look at this JSFiddle example for reference. I am currently working with tablesort and its associated CSS to achieve alternating row colors for the rows containing first and last names. Additionally, I want the rows within the sub-table disp ...

Selenium Python can be used to save a web page as a

Trying to print a webpage as a PDF with headers and footers to mimic the output from Google Chrome. Any suggestions? Experimented with PhantomJS but couldn't get it to include headers and footers. from selenium import webdriver import selenium def ...

Angular component unable to locate specified DOM element during lazy loading

I am currently working with a nebular stepper component and I have implemented lazy loading for the final step. Within this component, there is a need to target an element by its id and make some modifications to it. Below is the structure of the stepper ...

Angular Firebase Email Verification sent to an alternate email address

I am facing a challenge with my website that only accepts emails from a specific domain, such as a university domain. Users who want to sign up using Facebook or Google need to verify their university email, but I am struggling to find a way to send the ve ...

New button attribute incorporated in AJAX response automatically

data-original-text is automatically added in ajax success. Here is my code before: <button type="submit" disabled class="btn btn-primary btn-lg btn-block loader" id="idBtn">Verify</button> $(document).on("sub ...

How to Overlay a Semi-Transparent Object on a Background Video using CSS and HTML

Hey there, I'm encountering a puzzling issue with my website. I have a background video set up, and I wanted to overlay a floating textbox on top of it. However, no matter what I do, the background color and borders of the textbox seem to be hiding be ...

The background image is not displaying correctly on mobile devices

I'm struggling to make the image fit the entire screen on a mobile device using the code below. Interestingly, it displays correctly on my personal computer. #topContainer { height:1048px; width: 100%; padding: 0; margin: 0; } #div1 { ...

Developing a calendar UI with similar features and functionality to Google Calendar using Ionic 2

My journey with Ionic 2 began only one week ago and has been relatively smooth sailing thus far. However, I have hit a roadblock - I need to create a calendar interface for users to book time slots. Can anyone offer assistance on how to tackle this task? ...

A list containing various checkboxes

I have created a list with checkboxes in each item. I want to ensure that if any of the child checkboxes are checked, the parent checkbox is also automatically checked. Here's the HTML code: <input type="checkbox" id="parent"> <ul> ...

What is the technique for highlighting the exact data point on the XY am4chart when it is clicked?

I have been searching high and low for a solution to my problem but haven't had any luck. I am working with a traditional XY am4chart that has hundreds of data points in the line series. What I want is for the chart to display a vertical line (or some ...