How to bind select element to an object in Angular ?
Last Updated :
24 Apr, 2025
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. An Angular service is a broad category that consists of any value, function, or feature that an application needs. A service is a class with a narrow and well-defined purpose. In this article, we will see how to bind a select element to an object in AngularJS. The process of binding a select element to an object helps in associating the selected value with a property in our component class.
Steps to bind select element to object: The below steps will be followed for binding the select element to the object in Angular:
Step 1: Create a new Angular project and install the necessary dependencies
ng new my-app
cd my-app
Step 2: Create a new Angular component
ng generate component my-component
This will create a new directory called "my-component" in your project's "src/app" directory, along with the necessary files for an Angular component.
Step 3: Create an interface or class to define the structure of the options
export interface Option {
value: string;
displayText: string;
}
Step 4: Define an array of options in your component
export class MyComponentComponent {
options = [
{ id: 1, name: 'Java' },
{ id: 2, name: 'C++' },
{ id: 3, name: 'Python' }
];
selectedOption: any;
}
Step 5: Use the ngFor directive to generate the options in the select element
<select [(ngModel)]="selectedOption">
<option *ngFor="let option of options"
[ngValue]="option">
{{option.name}}
</option>
</select>
Step 6: Use the selected option in your component
<p>You selected: {{ selectedOption | json }}</p>
We will understand the above concept with the help of suitable approaches & will understand its implementation through the illustration.
Approach 1: In this approach, we are using the ngValue directive to bind the entire object to the value attribute of the option element. This approach requires a compareWith function to be defined to properly compare the selected option with the options in the array.
Example 1: Below example demonstrates the binding of the select element to an object in angular.
JavaScript
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { MyComponentComponent }
from './my-component/my-component.component';
@NgModule({
declarations: [
AppComponent,
MyComponentComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
- my-component-component.html
HTML
<h2 style="color: green">
GeeksforGeeks
</h2>
<select [(ngModel)]="selectedOption">
<option *ngFor="let option of options"
[ngValue]="option">
{{ option.name }}
</option>
</select>
<p>
You selected: {{ selectedOption | json }}
</p>
- my-component-component.ts
JavaScript
import { Component } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent {
options = [
{ id: 1, name: 'Java' },
{ id: 2, name: 'C++' },
{ id: 3, name: 'Python' }
];
selectedOption: any;
}
HTML
<app-my-component></app-my-component>
Output:
 Approach 2: In this approach, we are using the value attribute of the option element to bind the entire object to the selectedOption property. This approach does not require a compareWith function and is simpler to set up.
Example 2: This is another example that demonstrates the binding of the select element to an object in Angular.
JavaScript
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { SelectObjectComponent }
from './select-object/select-object.component';
@NgModule({
declarations: [
AppComponent,
SelectObjectComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
- select-object.component.html
HTML
<h2>My Component</h2>
<select [(ngModel)]="selectedOptions">
<option *ngFor="let option of options"
[value]="option.name">
{{ option.name }}
</option>
</select>
<p>
Selected Option: {{ selectedOptions | json }}
</p>
- select-object-component.ts
JavaScript
import { Component } from '@angular/core';
@Component({
selector: 'app-select-object',
templateUrl: './select-object.component.html',
styleUrls: ['./select-object.component.css']
})
export class SelectObjectComponent {
options: any = [
{ id: 1, name: 'HTML' },
{ id: 2, name: 'CSS' },
{ id: 3, name: 'Javascript' },
{ id: 4, name: 'MongoDb' }
];
selectedOptions: any
}
HTML
<app-select-object></app-select-object>
Output:
Â
Similar Reads
How to select an element by its class name in AngularJS?
Given an HTML document and the task is to select an element by its className using AngularJS. The elements can be selected with the help of a class name using the document.querySelector() method that is used to return the first element that matches a specified CSS selector(s) in the document. Approa
2 min read
How to select first object in object in AngularJS?
The main problem that we are dealing with is that for an object of objects reading the object of a particular index position is not as simple as a list. We cannot loop over it using ngFor as an object is not considered an iterable. The importance of this issue may arise when the data received from a
3 min read
How to filter by object property in AngularJS?
Filtering by object property in AngularJS is the concept of choosing the specific objects from the data array which is based on the individual properties. In creating a web application, this is the most common task that deals with the data that needs to be sorted and then displayed to the user. Deve
6 min read
How to set id attribute of an element dynamically using AngularJS ?
In this article, we will see how to change the ID of any element dynamically using AngularJS, along with understanding its implementation through examples.Approach 1: In this approach, a function is called when the button is clicked, which changes the ID of the element to id-5. We are calling a func
3 min read
How to render an Object in a Sorted order based upon Key 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. To achieve this, we can display the object's properties in a particular order, where the order is dete
3 min read
How to achieve Two-Way Data Binding in Angular with ngModel ?
Two-way Data Binding in Angular allows you to automatically synchronize data between a component class property and an input element (e.g., an input field) in your template. To achieve this binding, it's typically used with Angular [(ngModel)] Â directive. This is basically the combination of the Pro
3 min read
How to use Pipes within ngModel on input Element in Angular ?
The ngModel directive is a directive that is used to bind the values of the HTML controls (input, select, and textarea) or any custom form controls, and stores the required user value in a variable and we can use that variable whenever we require that value. In this article, we will learn about Usin
3 min read
How to pass data from Parent to Child component in Angular ?
We can use the @Input directive for passing the data from the Parent to Child component in AngularUsing Input Binding: @Input - We can use this directive inside the child component to access the data sent by the parent component. Here app.component is the Parent component and cdetail.component is th
2 min read
How To Get Select Element's Value In react-bootstrap?
React-Bootstrap is a popular library that provides Bootstrap-styled React components. One of the most commonly used form elements is the <select> dropdown, which allows users to choose from a list of options. In this article, weâll explore how to get the value of a select element in a React-Bo
2 min read
Why does AngularJS include an empty option in select ?
AngularJS is a Javascript framework containing several built-in functionalities, that intend to make the development easier and more efficient for the developers. The dropdown is one of them. A dropdown is a GUI element that enables the user to choose one value from a list of values. When a drop-dow
3 min read