How to set the selected value in multiselect dropdown using Angular

0 votes
How to set the selected value in multiselect dropdown using Angular?

I'm trying to set a selected value in a multiselect dropdown in an Angular application. Can someone guide me on how to achieve this?
Dec 12, 2024 in Web Development by Nidhi
• 11,580 points
113 views

1 answer to this question.

0 votes

Example with [(ngModel)] (Template-driven Forms)

1.       HTML:

<select [(ngModel)]="selectedValues" multiple>

  <option *ngFor="let option of options" [value]="option">{{ option }}</option>

</select>

2.       Component:

import { Component } from '@angular/core';

@Component({

  selector: 'app-multiselect',

  templateUrl: './multiselect.component.html',

})

export class MultiselectComponent {

  options = ['Option 1', 'Option 2', 'Option 3', 'Option 4'];

  selectedValues = ['Option 2', 'Option 3']; // Pre-selected values

}

Example with Reactive Forms

1.       HTML:

<form [formGroup]="form">

  <select formControlName="multiSelect" multiple>

    <option *ngFor="let option of options" [value]="option">{{ option }}</option>

  </select>

</form>

2.       Component:

import { Component, OnInit } from '@angular/core';

import { FormGroup, FormControl } from '@angular/forms';

@Component({

  selector: 'app-multiselect',

  templateUrl: './multiselect.component.html',

})

export class MultiselectComponent implements OnInit {

  options = ['Option 1', 'Option 2', 'Option 3', 'Option 4'];

  form: FormGroup;

  ngOnInit() {

    this.form = new FormGroup({

      multiSelect: new FormControl(['Option 2', 'Option 3']), // Pre-selected values

    });

  }

}

answered Dec 12, 2024 by Navya

Related Questions In Web Development

0 votes
1 answer

How do you get the value of a selected option in a dropdown using jQuery?

To get the selected value of an ...READ MORE

answered Nov 13, 2024 in Web Development by kavya
160 views
0 votes
1 answer

How to set a cookie in Node.js using the Express framework?

You can use the res.cookie() method to ...READ MORE

answered Nov 27, 2024 in Web Development by Navya
127 views
0 votes
1 answer

How to set selected value of jQuery Select2?

To set selected value of jQuery Select2 ...READ MORE

answered Aug 5, 2022 in Web Development by rajatha
• 7,680 points
7,212 views
0 votes
0 answers

How to get the value of a checkbox flipswitch in JQuery Mobile 1.4.5?

I'm using following markup to create a ...READ MORE

Aug 22, 2022 in Web Development by gaurav
• 23,260 points
690 views
+1 vote
8 answers

How can I implement process.env in Angular 5 environment?

Users do not have access to process.env ...READ MORE

answered Apr 3, 2018 in DevOps & Agile by DareDev
• 6,890 points
13,469 views
0 votes
1 answer
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
2,060 views
+4 votes
9 answers

***IMPORTANT*** AngularJS Interview Questions.

Yes, I agree with Omkar AngularJs is ...READ MORE

answered Mar 17, 2019 in Career Counselling by Sharad
• 180 points
3,954 views
0 votes
1 answer

How to dynamically change meta tags before the site is scraped in Angular 2?

To dynamically change meta tags before Angular ...READ MORE

answered Nov 6, 2024 in Web Development by kavya
339 views
0 votes
1 answer

How to Align a Home Button and Dropdown Menus on the Same Line in a Navigation Bar?

Using CSS Flexbox, you can center a ...READ MORE

answered Dec 12, 2024 in Web Development by Navya
97 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP