How to use ngfor to make a dropdown in Angular from an array ? Last Updated : 25 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this post, we will see how we can display the array elements in the drop-down menu using AngularJS. Sometimes we need to display dynamically fetched data and this is where features ngFor come into the scene. We can iterate over the array, apply conditions and display the data easily.Using ngFor: NgFor is a built-in template directive that makes it easy to iterate over something like an array or an object and create a template for each item.Syntax:<tag-name *ngFor="let item of array">{{iter}}</tag-name><tag-name *ngFor="let key of object">{{key}}</tag-name>Prerequisites: NPM must be preinstalled.Environment Setup:Install angular :npm install -g @angular/cliCreate a new Angular project:ng new <project-name>cd <project-name> Check the installation by running the project. You should see the angular landing page on http://localhost:4200/ng serve -oCreating drop down menu: 1. Create a new component: ng g c dropdown 2. It will create a new directory with 4 new files. Open dropdown.component.ts and paste the following code: dropdown.component.ts: JavaScript import { Component } from '@angular/core'; @Component({ selector: 'app-dropdown', templateUrl: './dropdown.component.html', styleUrls: ['./dropdown.component.css'] }) export class DropdownComponent { players = [ "Sachin Tendulkar", "Ricky Ponting", "Virat Kohli", "Kumar Sangakkara", "Jacques Kallis", "Hashim Amla ", "Mahela Jayawardene ", "Brian Lara", "Rahul Dravid", "AB de Villiers" ] selected = "----" update(e){ this.selected = e.target.value } } In above code, we have defined players array that contains the data that we will display in the drop down menu. Additionally we have a selected variable that we will use to display the selected element. The method update() takes an event and sets selected to its value. 3. Now add the following code into dropdown.component.html:dropdown.component.html: HTML <h3>Choose Your Favorite Cricket Player</h3> <select #cricket (change)="update($event)"> <option value="default">----</option> <option *ngFor="let player of players" [value]="player"> {{player}} </option> </select> <p>You selected {{selected}}</p> We have created a drop-down menu that will use the players array. The options are populated using ngFor. The selected variable is used to display the selected option. 4. Finally add this component into app.component.html: app.component.html: HTML <app-dropdown></app-dropdown> 5. Now run the project and open http://localhost:4200/ to see the results: ng serve -oOutput Comment More infoAdvertise with us Next Article How to use ngfor to make a dropdown in Angular from an array ? M mukulbindal170299 Follow Improve Article Tags : Web Technologies HTML AngularJS HTML-Questions AngularJS-Questions +1 More Similar Reads How to Filter an Array based on user input in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. Angular filters can be added in AngularJS to format data. 3 min read How to create a hyperlink for values from an array in Angular 8? Introduction:In angular 8, we can create hyperlinks for all the values that are present in the array using *ngFor. Approach:1) First declare and initialize array in .ts file.2) Then use *ngFor directive for iterating through the array.3) Using this *ngFor directive in .html file, we can use it as pe 1 min read How to Sort an Array based on User Input in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. In this article, we will see how to sort an array based o 3 min read How to create a To-Do list using Drag and Drop in Angular 7 ? We can easily create a To-Do list using Drag-Drop module provided by angular Component Development Kit (CDK). First of all, create an angular component by using the following command- ng g c To-Do Now import CdkDragDrop, moveItemInArray, transferArrayItem from @angular/cdk/drag-drop to our to-Do com 2 min read How to make Dropdown using Angular UI Bootstrap ? In this article, we will see how to make Dropdown using Angular UI bootstrap. Angular UI Bootstrap is an Angular JS framework created by Angular UI developers for providing better UI which can be used easily. Syntax: <div uib-dropdown></div> Download AngularUI from the link: https://angu 2 min read How to print an array in table format using angularJS? Given an array & the task is to print the given array in the tabular format using AngularJS. In JavaScript, data can be stored in the form of arrays. Each of the array items has unique indexing, starting from 0. But what if the developer wants to display all the items that are in the array, on t 4 min read How to make a multi-select dropdown using Angular 11/10 ? In this article, we will learn to build the multiple selection drop-down menu in Angular. To accomplish this task, we require Angular 10 or the Angular 11 version. Sometimes we need to display dynamically fetched multi-selected data in a drop-down menu, for this, we will use the npm @ng-select/ng-se 3 min read How to apply filters to *ngFor in Angular ? In this article, we will see How to apply filters to *ngFor in AngularJS, along with understanding their basic implementation through the examples. NgFor is used as a Structural Directive that renders each element for the given collection each element can be displayed on the page. Implementing the f 3 min read How to set a Selected Option of a Dropdown List control using Angular ? In Angular, a select or dropdown list is a common UI component that allows users to choose a single value from a predefined set of options. Angular select allows us to bind the dropdown list to a data source, such as an array or an object, using directives like `*ngFor`. It allows 2-way data binding 3 min read How to Sort an Array of Objects by String Property value in Angular ? An Object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In this article, we will learn how to Sort an array of objects by string property value in Angular. He 3 min read Like