Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

How to use ngFor and ngIf on same element in angular 12 ?

Aman Jain, October 23, 2021October 23, 2021

In this tutorial we are going to resolve the issue of using *ngFor and *ngIf on same element. so, in angular ngIf and ngFor is a structural directives, and we can not use two structural directive on same element.ngIf is used for to make conditional logic and ngFor is used for to repeat the element.

You can also read about What is directives and types of directive in angular? and what is structural directives in angular.

Why we can’t use two structural directive on same element in angular 12?

In angularjs (angularjs is first version of angular) we can use both on same element but not in angular 2 because Angular unable to decide which one to use first for example if we are using ngIf and ngFor together.

<div *ngIf='place.id>10' *ngFor="let place of places">
   <p>{{place}}<p>
</div>

In the above example, if angular try to execute first ngIf then place is undefined for it.

So, above code will throw below exception

close

Error

Can’t have multiple template bindings on one element. Use only one attribute prefixed with *

So, Now come to the main point.

To use ngFor and ngIf on element we can create parent child strategy see below example:

<div *ngIf='place.id>10'>
 <div  *ngFor="let place of places">
   <p>{{place}}<p>
 </div>
</div>

But in above example it will create a extra div so to eliminate this we can use ng-container

<ng-container *ngIf='place.id>10'>
 <div  *ngFor="let place of places">
   <p>{{place}}<p>
 </div>
</ng-container>

Related

Javascript Angular

Post navigation

Previous post
Next post

Related Posts

jQuery redirect after Ajax success Laravel PHP

December 4, 2021December 4, 2021

In this article i will show you to redirect a page after successful response from Ajax request. Laravel or PHP provides itself to redirect a page using its own methods but while working with JavaScript Ajax Laravel methods are not useful because it won’t redirect the page. so in this…

Read More
Javascript Angular KeyUp Event on Input Example

Angular KeyUp Event on Input Example

September 22, 2022March 16, 2024

Angular KeyUp Event on Input is an Angular event binding it triggers when user interacts with DOM like text based input element and when we key up the input. Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular KeyUp…

Read More
Javascript Angular Select Change Event Example

Angular Select Change Event Example

September 23, 2022March 16, 2024

Angular Select Change Event is an angular event binding it triggers when user change the value of select box and it fires a event of (change). Select events in angular can be handled using input and output decorators and in this tutorial we will learn Angular change Event on select…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version