How to Bind Select Element to Object in Angular?

By Hardik Savani | April 5, 2020 | 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 and angular 10.

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




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: