Manipulating the distinct look of the final element in an *ngFor loop

I am trying to enhance the appearance of the last line of controls generated by an iterator by making it disabled and somewhat invisible. Currently, my code is functioning well, as shown below.

<div *ngFor="let item of data; let last = last;">

  <input class="form-control"
        [value]="item.e"
        [disabled]="last">

  <button (click)="onRemove(item)"
          class="btn btn-outline-danger"
          [style.visibility]="last?'hidden':''">Do</button>

</div>

I want to improve the quality of my code, particularly the management of visibility aspect. The disability setting looks good, but the visibility conditional seems cumbersome.

What would be a better way to handle visibility control in a more elegant manner?

I attempted the following approach, but it did not work.

<button (click)="onRemove(item)"
        class="btn btn-outline-danger"
        [style.visibility.hidden]="last">Do</button>

Answer №1

If you're looking to hide an item, consider creating a CSS class specifically for that purpose.

.hidden {
  display: none;
}

Then, use this class based on certain conditions.

<button (click)="onRemove(item)" 
        class="btn btn-outline-danger"
        [class.hidden]="last">
</button>

You can see an example in action at this stackblitz link.

Answer №2

To achieve this, you can use the syntax

[style.visibility]="last && 'hidden'"
.

Essentially, if the variable last is considered truthy, then last && 'hidden' will result in 'hidden'; otherwise, it will evaluate to a falsy value. When setting a style using [style.property], if the value is falsy, the property will not be applied.

If you want to learn more about truthy and falsy values, check out this resource. Additionally, explore how short-circuit logic can offer a more elegant way to evaluate values here.

Overall, your solution appears clean and clear, which is important for readability by other developers who may come across your code.

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

Fixing content at the bottom inside a container with Bootstrap 4

My app is contained within a <div class="container">. Inside this container, I have an editor and some buttons that I always want to be displayed at the bottom of the screen. Here's how it looks: <div class="container> // content here.. ...

Using AngularJS to bind radio buttons to ng-model

Here is a snippet of my HTML code where I attempted to bind the "formFriendsAll" scope to the ng-model. <form ng-submit="submitForm()" > <div class="col-sm-3"> <div class="form-group"> <label>Which Persons to show?< ...

rxjs - straightforward issue with initiating observables

I'm currently tackling an assignment that involves setting up a basic form with input fields for 'From', 'To', and 'Duration' using rxjs. While it might be easier to just utilize getter / setters, I'm determined to ...

Determine the overall price of the items in the shopping cart and automatically show it at the bottom without the need for manual entry

Creating a checkout form that displays the total cost of products, subtotal, GST cost, delivery cost, and overall price. The goal is to calculate the total by adding together the costs of all products with the "price" class (may need ids) at a 15% increase ...

"An issue occurred while trying to utilize the output received from the angular service (undefined

I currently have two variables declared in my Typescript class: private myServiceSubscription: Subscription; myVar: myDto[] = []; Within the constructor: this.myServiceSubscription = this.deliveryPointService .getPostalAddresses() .subsc ...

Steps to eliminate the gap between the link element and toggler in Bootstrap 4

Incorporating the code below will create a navigation bar with a cart icon in Bootstrap 4, indicated by the images. The expanded view showcases the cart next to the search section on the right. When viewed on smaller devices in collapsed mode, it should al ...

Creating Visuals from Text with ReactJS/NextJS

Looking to transform text into an image with a shareable link in ReactJS, similar to Spotify's lyrics sharing feature. I haven't attempted any solutions so far. I've explored various libraries without success. ...

unable to display toolbar in ckeditor5 decoupled document with Angular

Having trouble implementing CKeditor5 decoupled document in Angular-10 and unable to display the toolbar. Component.ts import * as CKeditor from '@ckeditor/ckeditor5-build-decoupled-document'; export class MyComponent { editor = CKeditor } ...

Customizing Material UI Popover CSS classes within a Select component in a React application

I have integrated the Material UI Select component into my React project. I am attempting to customize the CSS classes .MuiPaper-root and/or .MuiMenu-list. This is how my Select component looks: <Select value={selectValue} disableUnderline onCha ...

Incorporating accordion functionality using jQuery within my Model-View-Controller

Struggling to integrate accordion jquery with my MVC view. I want it to show content when @item["name"] is selected, but currently it's not working. Here is the script I'm using: <script> $(function () { $("#parameter_accordion").acco ...

How can I align rectangles with different heights to display side by side using Javascript?

Currently, I am designing a press page for a website where the headlines/articles are displayed in rectangles. To achieve this layout, I am using the following CSS: .press-blocks{ column-count: 4; column-gap: 2em; padding-left: 10%; padding ...

Is there a way to achieve a straightforward three-column stacked layout in Bootstrap 5 that spans 100% of the viewport height?

Today I decided to try out Bootstrap 5 for the first time. However, I am facing a challenge in creating a 3 stacked column layout with box-1, box-2, and box-3 on top of each other, taking up 100vh. I have shared a redacted version of the code below, wher ...

Tips for modifying link height using CSS

I'm currently facing an issue with adjusting the height of "footer a" links that are using Fontface vector icons in the footer. I have added a red border around them to visually identify the problem. Ideally, when navigating through the links, the bor ...

The UI elements are failing to reflect the changes in the data

In an attempt to establish communication between three components on a webpage using a data service, I have a Create/Edit component for adding events, a "next events" component for accepting/declining events, and a Calendar component for displaying upcomin ...

Steps for positioning a RadioButtonList at the center of a table cell

How can I easily center a RadioButtonList? It used to be straightforward, but for some reason the below HTML is not working. Can you spot what's missing? <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></ ...

The footer element is missing from the body section of the HTML code, causing the box shadow to not encompass the footer as intended

Having a problem in CSS where the footer is not appearing within the body element in the HTML code. The footer is placed between the body tags, but the box shadow defined for the body is not being applied to the footer. The code snippet below shows the CSS ...

"Styling with CSS: Create a sleek border-bottom and background effect

I want to add a bottom border and background color on mouse over. Almost there! In the example below, hovering over the span gives an underline. However, I need the underline to display when entering the hyperlink area. a{ text-decoration: none; paddin ...

problem encountered when inserting data (GET method) in a loop using window.location

I'm currently utilizing sailsjs to extract data from the GET parameter within the URL (mydomain.com/prod_master/addsupp). The specific page is /prod_master/addsupp, designed to receive GET parameters for database insertion. In my Javascript code, I ...

Error: Unable to connect to 'ngbTypeahead' as it is not recognized as a valid attribute of 'input' in the template

Having trouble with ngbTypeahead. The console is displaying the following error message: Error: Template parse errors: Can't bind to 'ngbTypeahead' since it isn't a known property of 'input'. (" <inpu ...

Testing the Limits of CSS Hover

I'm having trouble figuring out how to implement this. When hovering, a yellow square/background should appear with an offset similar to the one in the image below. I am utilizing bootstrap for this project. Any assistance or guidance would be greatl ...