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:
.gif)
Similar Reads
How to Scroll to an Element on click in Angular ?
In this article, we will see how to scroll to an element on click in Angular. Here, we will create a component that enables scrolling to specific targets when a button is pressed within the document from one target to another. Steps for Installing & Configuring the Angular ApplicationStep 1: Cre
4 min read
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 update an array element in AngularJS ?
Given an array with an initial array of elements & the task is to update an array element using AngularJS. To update a particular item in an array, there are 2 ways, i.e., either by its value or by its index. Here, we will use the concept of the Property accessors method that is used to access t
2 min read
How to push elements in an array using AngularJS ?
Given an array and the task is to perform the push operation on the array using AngularJS. The arr.push() method is used to push one or more values into the array, & new values will add at the end of an array. This method changes the length of the array by the number of elements added to the arr
2 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 fun
3 min read
How to show a span element for each rows clicked in AngularJS ?
In this article, we will see how to display a span element for each row clicked in AngularJS. A span element is used to group in-line elements in a document. It can be used to make a hook to a particular part of the document which may be subject to a particular action based on DOM events. The span e
4 min read
How to check the existence of key in an object using AngularJS ?
Given an object containing a (key, value) pair and the task is to check whether a key exists in an object or not using AngularJS. In order to check the existence of a key in an object, we will create an object having properties in the form of a key: value pair. Define a temporary variable that will
2 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