How to Bind Select Element to Object in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. 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, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Pass Multiple Parameters to Pipe in Angular?
- Angular Nl2br Pipe Example
- How to Set HTML Meta Tags in Angular?
- Angular 9/8 SEO - How to Add Page Title and Meta Tags?
- Angular Checkbox Change Event Example
- Angular Check ngIf Null or Empty | Angular Check If Array is Empty
- Angular NgStyle Example | NgStyle Directive In Angular
- Angular NgIf Example | NgIf Directive In Angular