How to Bind Select Element to Object in Angular?

By Hardik Savani October 20, 2023 Category : Angular

In this article we will cover on how to implement Binding Select Element to Object in Angular. In this article, we will implement a angular select element bind to object. you will learn bind select element to object in angular.

In this article, we will implement a select box bind object in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17.

Sometime we need to bind select element to object so when you run change event then you can easily get selected option object and perform some action on it. So let's see bellow example.

Example

src/app/app.component.ts

import { Component } from '@angular/core';

interface category{

id:number;

name:string;

}

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

name = 'Angular';

selectedObject : category;

categories = [

{id: 1, name: 'JQuery'},

{id: 2, name: 'Angular'},

{id: 3, name: 'Vue'},

{id: 4, name: 'React'}

]

}

src/app/app.component.html

<h1>Binding Select Element to Object in Angular - ItSolutionStuff.com</h1>

<select [(ngModel)]="selectedObject">

<option *ngFor="let cat of categories" [ngValue]="cat">

{{cat.name}}

</option>

</select>

{{selectedObject | json}}

You can see bellow output:

I hope it can help you...

Tags :
Shares