Showing off the latest products at the top of the list

Typically, when utilizing ngFor, the most recent item is displayed underneath the initial element.

For instance, a list containing: [Apple, Orange, Banana]

If we use ngFor to display this list:

  • Apple
  • Orange
  • Banana

I am interested in learning a method to reverse the order and showcase the very last element (or any newly added elements that appear at the top while the older ones are automatically shifted below). For example:

  • Banana
  • Orange
  • Apple

HTML:

<button (click)="deposit()">Deposit</button><br>
<button (click)="withdraw()">Withdraw</button>
<p>{{balance}}</p>

  <p *ngFor="let element of transactions">{{element}}</p>


  <router-outlet></router-outlet>

CSS:

.example {
    background-color: #eee;
    width: 200px;
    height: 100px;
    border: 1px dotted black;
    overflow: scroll; /* Add the ability to scroll */
    -ms-overflow-style: none;
    scrollbar-width: none;
    direction: ltr;
  }

  .example::-webkit-scrollbar {
    display: none;
}

I would appreciate suggestions for a CSS attribute that causes the oldest elements below the overflown list to gradually fade away and only become visible when scrolling down.

Answer №1

I'm not completely sure what your goal is, but if you're looking to maintain the original sequence while displaying the last element first, you can achieve this by using array slicing to extract the last element:

<p>{{transactions[transactions.length - 1]}}</p>
<p *ngFor="let element of transactions.slice(0, transactions.length - 2)">{{element}}</p>

If you prefer to completely reverse the order, you can do so with this code:

<p *ngFor="let element of transactions.slice().reverse()">{{element}}</p>

Answer №2

To achieve this effect, you can utilize CSS.

<div class="wrapper">
  <p *ngFor="let item of data">{{item}}</p>
</div>
.wrapper {
  display: flex;
  flex-direction: column-reverse;
}

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

release a Node.js module on NPM

Being a complete beginner in creating npm packages using typescript2 and angular2, I find myself in need of creating an npm package and publishing it on our company's private repository. I've managed to generate files like d.ts and .js. But how ...

Space left vacant following an unordered list item

Is there a way to remove unwanted space after an unordered list in IE and Firefox, specifically in the last HTML <li> item? I've tried adjusting the ul width but it didn't work. Any ideas on how to get rid of the space? Thanks. #menubar ...

Disguising the Navigation Bar when choosing from various databases

I am currently facing the following issue: <div class="container"> <h3 class="d-flex justify-content-center">Database</h3> <div class="row"> <div class="col-xs-12"> < ...

What is the process for defining an opaque type in programming?

[ This is not this ] Take a look at this snippet of code: interface Machine<OpaqueType> { get(): OpaqueType, update(t: OpaqueType); } const f = <U, V>(uMachine: Machine<U>, vMachine: Machine<V>) => { const u = uMach ...

Unable to pass a parameter through an Angular http.get request

I've encountered an issue where I am attempting to pass the page number and page size values to a web API, but for some reason, no parameters are being passed. I have thoroughly debugged the application in VS Code, and verified that the pagingModel ob ...

Issue with IE11 compatibility in Angular2 (AngularQuickStart version 2.4.0) due to syntax error

Encountering an error in the browser console when attempting to run my Angular2 app on IE11. The error "Недопустимый знак" translates to unacceptable symbol. https://i.stack.imgur.com/0mHBC.png Here is a snippet of my index.html: <!DO ...

Unexpected outcome in Angular unit testing

I am new to Angular unit testing using Jasmine and Karma. I have created a simple component for testing purposes. Here is my component code: import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-input&apos ...

When the mouse button is released or when an event listener is

I've been pondering a question that has yet to be fully answered. When I implement this technique to catch a mouse up event: <div onmouseup="/*Script to be executed*/"></div> Is it more efficient than this newer approach: <div id=" ...

Angular 9: The instantiation of cyclic dependencies is not allowed

After transitioning from Angular 8 to Angular 9, I encountered an issue with a previously functioning HTTP communication service. The error message now reads: Error: Cannot instantiate cyclic dependency! HttpService at throwCyclicDependencyError (core ...

Encountering a parse error when parsing JSON using getJSON

Looking to incorporate some jquery.gantt and it requires data in JSON format. The documentation can be found at () Here is the jQuery code snippet: $(".gantt").gantt({ source: basePath + "system/print_gantt_project_data.php?project_id=" + pro ...

What are some tips for getting media queries to function properly?

I need help with my homepage banner image as I am trying to make it responsive by adding media queries. The goal is for the image to adapt to 3 different sizes based on the screen being used, but currently only the medium-sized version appears. Could someo ...

Error in Angular 5: Cannot find property 'then' in type 'Observable<any>'

I encountered the following error message: "[ts] Property 'then' does not exist on type 'Observable'. How can I resolve this issue? Below is my Component code: getUsers(){ this.authService.getUsers().then((res) => { thi ...

Which option's value was recently removed from the fetch command?

I created a function to handle duplicate selections in a select element. It is functioning properly, but I noticed that when I remove an option from the select, my array remains unchanged. If I remove an option, I want my code to detect the value of that ...

Building a .NET Core 3.1 application that integrates SQL Server 2019 Express for managing multiple databases, including a main database dedicated to

I'm currently developing a web application using .NET Core 3.1 and Angular 9. I am curious to know if it is feasible to leverage the internal authentication/authorization system in .NET Core to connect to an "authorization" database. This would allow ...

Is it possible to select an input field while using jQuery animate?

This query arises indirectly from a previous question regarding an animation script for a form. The script in question is a basic jQuery function that expands the width of an input field when it is focused on and reverts to its original width when unfocus ...

Setting up Scss and purgeCss configuration in Next.js custom postCSS configuration: A step-by-step guide

My current project is using Scss in combination with Bootstrap for design. I have implemented purgeCss to remove unused Css, and customized my postcss.config.js file as follows: module.exports = { plugins: [ "postcss-flexbugs-fixes", [ " ...

Creating rows within a table in React.js using the map method: Techniques to follow

Here is my code snippet: const [tasks, setTasks] = useState(''); I am simulating data with a mock server. function fetchTasks() { axios.get('http://localhost:4000/tasks') .then(function (response) { ...

Develop a custom dropdown menu using JavaScript

I've been working on creating a dropdown menu that appears after selecting an option from another dropdown menu. Here's the HTML code I'm using: <br> <select id ="select-container" onchange="addSelect('select-container') ...

What are the steps for customizing the interface in TypeScript?

After fixing a type error related to adding custom functions to the gun chain by including bind():any within IGunChainReference in @types/gun/index.ts, I am wondering how to transfer this modification to one of my project files. I have not been able to fi ...

Can a filter be eliminated from a CSS file in a modified CSS file?

Currently, I am facing a dilemma with a CSS rule in my file. The rule in question looks like this: .someElement { filter:gray(enabled=true) alpha(opacity=50); -ms-filter:"gray(enabled=true) alpha(opacity=50)" } This specific rule is causing some ...