ItSolutionStuff.com

How to Bind Select Element to Object in Angular?

By Hardik Savani • May 2, 2024
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: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Pass Multiple Parameters to Pipe in Angular?

Read Now →

Angular Nl2br Pipe Example

Read Now →

How to Set HTML Meta Tags in Angular?

Read Now →

Angular 9/8 SEO - How to Add Page Title and Meta Tags?

Read Now →

Angular Checkbox Change Event Example

Read Now →

Angular Check ngIf Null or Empty | Angular Check If Array is Empty

Read Now →

Angular NgStyle Example | NgStyle Directive In Angular

Read Now →

Angular NgIf Example | NgIf Directive In Angular

Read Now →