How to Bind Select Element to Object in Angular?

How to Bind Select Element to Object in Angular?

In this post we will give you information about How to Bind Select Element to Object in Angular?. Hear we will give you detail about How to Bind Select Element to Object in Angular?And how to use it also give you demo for it if it is necessary.

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

Also see:How to Get Index of Ngfor in Angular?

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

Hope this code and post will helped you for implement How to Bind Select Element to Object in Angular?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

36  +    =  42

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US