Show a placeholder message if the array does not contain any elements

Here's the html code snippet I am working with

 <empty-list [hidden]="!emptylist" text="There is currently No User"</empty-list>
  <div *ngFor="let userObser of userObservables">
    <ion-item #emptylist *ngIf="userObser | async as user">   
        <h2>{{user.displayName}}</h2>
        <h3>{{user.email}}</h3>
    </ion-item>
  </div>

I aim to display the empty list only when there are no users and hide it if at least one user exists.

While I can achieve this using a subscribe method, I prefer utilizing the async pipe to avoid the need for manual unsubscription each time. This seems inefficient to me.

Is it possible to create a local variable within the ion-item element that could be referenced outside to control visibility based on its existence? I have tried implementing this idea but haven't had any success yet.

Answer №1

In order to display an alternative template when a condition is not met, you can incorporate a simple else statement within the ngIf block:

<ng-container *ngIf="userObservables.length > 0; else emptyList">
  <ng-container *ngFor="let user of userObservables">
    <ion-item *ngIf="user | async as user">…</ion-item>
  </ng-container>
</ng-container>

<ng-template #emptyList>
  <empty-list>…</empty-list>
</ng-template>

It's worth mentioning that I have made use of a ng-container instead of a div element to prevent unnecessary DOM nodes from being created.


On a related note regarding your own proposal: keep in mind that template references are only accessible within the template boundary. Structural directives such as *ngIf and *ngFor establish their own templates, thus establishing a boundary. This is why

<h2>{{ ref.innerText }}</h2>
<ng-container>
  <span #ref>Hello, World</span>
</ng-container>

will function correctly, while

<h2>{{ ref.innerText }}</h2>
<ng-container *ngIf="true">
  <span #ref>Hello, World</span>
</ng-container>

will not produce the desired outcome.

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

Choosing a table cell in a React Bootstrap table

export class UsersTable extends React.Component { constructor() { super(); this.state = { data: null }; } componentWillMount() { fetch("http://localhost:8081/users/getData") .then(res => res.json()) . ...

What is the best way to include the title "addmoves" to the server using AngularJS?

https://i.sstatic.net/yR2fk.png Furthermore, I am looking to include a button that allows users to add additional movies. The goal is to input multiple sets of data simultaneously, such as: newMovies = [ { movieName:"", director:"", release ...

Is there a way to access a header value using nodejs and express?

Is there a way to retrieve the x-api-key value sent in the header of my endpoint using the GET method? The code works fine on my localhost but it throws an error on the server. Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the ...

Wait for the response from a post request in Angular and retrieve the value once it becomes accessible

In my current method, I am sending data to the backend and receiving information about any mutations that occurred (specifically, how many new rows were added to the database). However, when using this method in its current state, the response holds the de ...

Is the video failing due to excessive adjustments of the video's currentTime?

Utilizing both user drag event and keypresses to adjust the position in an HTML5 video element, I am updating the video time with the following line of code: video.currentTime = toTime; Additionally, I am updating a canvas based on the video's posit ...

Tips for creating a more seamless box transition within HTML

I've been searching for information on this topic, but I haven't been able to find a satisfactory answer. I want to create a div that displays "Contact Us" and when clicked, smoothly reveals a layer with input fields. I know I can use JavaScri ...

Modifying the geometry of a three.js object

I have been working on changing the geometry of objects in a three.js scene. I have written code that almost works, where a mouse click triggers the change. However, I am facing an issue where the shape is only updated on the first mouse click, even though ...

The Element UI feature ensures that duplicate entries are not added to the table through the use of

Within my form created with the Element UI, I have implemented an autocomplete feature. However, I am facing an issue where duplicate entries are being added to the table even if they already exist. My goal is to prevent the addition of duplicate entries a ...

The Javascript code for sleep execution is operational, yet it appears to be not inducing any noticeable delays

This Angular app contains code that runs inside a webworker written in Typescript. As someone new to working with webworkers, I have encountered an issue where both the sleep function and the loop seem to be executing within the same thread. The main goal ...

how to use jQuery to locate the immediately preceding node that meets specific criteria

How can I locate the nearest parent node above $(this) node that has a specific property such as class="field", without considering parent/child relationships? ...

Basic animation created using Three.js

I'm currently experimenting with a basic color animation in Three.js. Below is the code I am using: const geometry = new THREE.BoxGeometry(1, 1, 1); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geomet ...

The issue with Three JS is that concentrating on the avatar's perspective while it moves is not functioning properly

I am currently delving into the world of Three JS as a novice in 3D programming. Following a tutorial from the book , I am working on a game where an avatar navigates through trees. My current challenge involves getting the camera to continuously focus on ...

transforming AJAX JSON responses into arrays

I am currently encountering an issue where I receive a JSON object response from a Rails controller like so: "77.576343,12.964343,77.576413,12.963679,77.575233,12.96545,77.5760913,12.9657723,77.575217,12.965333" However, I require this data to be formatt ...

Tips for successfully retrieving a span value in VUE

I am working with forms <input type="text" v-model="email"> <span>Extracted Value</span> Can someone help me figure out how to pass the value from the span element? data () { return { email: '/*Here goes the extracted valu ...

Having trouble with Http.post in Angular4 cli when 'application/json' is set in the header

As a beginner in Angular CLI, I encountered issues with the login API 'http://localhost/appointjobs/index.php/admin_api/index' when using http.post. I was unable to receive post data on the server side (codeigniter/php) when setting 'content ...

Error encountered when attempting to run an Angular 2 application with 'ng serve' command

Initially, I installed angular-cli using the command npm install -g angular-cli and then proceeded to create an angular-cli project. Following that, I used the command ng new Angular2TestProject to create a project and changed the directory to Angular@Test ...

Is it Possible for Angular Layout Components to Render Content Correctly even with Deeply Nested ng-container Elements?

Within my Angular application, I have designed a layout component featuring two columns using CSS. Within this setup, placeholders for the aside and main content are defined utilizing ng-content. The data for both the aside and main sections is fetched fr ...

Iterating through HTML table data using a JQuery for each loop

Currently, I have an HTML table that is structured in the following way: <div class="timecard"> <h3>tommytest</h3> <table class="misc_items timecard_list" border="0" cellpadding="2" cellspacing="0" style="margin:0 auto;"> < ...

In Vue.js, you can utilize one property to access additional properties within the same element

Is it possible to access other properties of the same element using a different property in Vue.js? For example: Rather than writing it like this: <kpi title="some_kpi" v-if="displayed_kpi==='some_kpi'"></kpi> I ...

Issues with the functionality of the accordion feature when implemented with object-oriented JavaScript

https://i.sstatic.net/QABff.jpgI am currently working on implementing an accordion using object-oriented JavaScript. The issue I am encountering is that the toggle functionality does not work when I click. The desired output is to display content and chang ...