How to use ngfor to make a dropdown in Angular from an array ?
Last Updated :
25 Jul, 2024
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:
npm install -g @angular/cli
- Create 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 -o
Creating 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 -o
Output
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 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