Angular Bind Object to Select Element Example

April 26, 2020 | Category : Angular

Now, let's see tutorial of select box bind object in angular. you will learn angular select option binding object. you'll learn select option binding in angular. This article will give you simple example of angular select option binding. Let's get started with Binding Select Element to Object in Angular.

In this article, we will implement a select box bind object in angular 6, angular 7, angular 8 and angular 9.

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 - HDTuto.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...