How to Implement Select2 in Angular 7?

·

,
angular7

Here is the updated post link for select2 in Angular 9:

Hello guys, welcome to therichpost.com. In this post, I will tell you, How to Implement Select2 in Angular 7?

I was sitting and thinking and suddenly an Idea came to my mind and I wrote new post related select2 implementation in Angular 7.

Select2 is very well customize select box with searching, sorting, autocomplete and many more features.

Here is the working picture:

How to Implement Select2 in Angular 7?

 

Here are the working coding steps:

 

1. Very first, you need to run common below commands to add Angular 7 project on your machine:
$ npm install -g @angular/cli 

$ ng new angularselect2 //Install Angular Project

$ cd angularselect2 // Go to project folder

$ ng serve //Run Project

http://localhost:4200/ //Run On local server

 

2. Now you need to run below commands to add select2 and jquery into your Angular application:
npm install select2

npm install jquery --save

 

3. Now add below code into your angular.json file:
...
"styles": [
              "src/styles.css",
              "node_modules/select2/dist/css/select2.min.css", 
              "node_modules/select2/dist/js/select2.min.js" // I added here because this is not working in script block
          ],
"scripts":[
              "node_modules/jquery/dist/jquery.js"
          ]
...

 

4. Now add below code into your app.component.ts file:
import { Component, OnInit } from '@angular/core';
import * as jquery from 'jquery';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angularselect2';

  ngOnInit()
  {
  	 jquery('.js-example-basic-single').select2(); //initialize select2 to particular input
  }
}

 

5. Finally add below code into your app.component.html file:
<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
  <select class="js-example-basic-single" name="state" style="width: 150px;">
  <option value="AL">Alabama</option>
  <option value="WY">Wyoming</option>
</select>
</div>

This is it. If you have any query related to this post then do comment below or you can ask questions.

Thank you,

Jatt Blood,

TheRichPost.

Notes: I have this blog for helping others and everyone can ask me queries related to Angular, Laravel, WordPress etc. I will be very happy to help all.

Here you can check more post related to select 2:

Comments

40 responses to “How to Implement Select2 in Angular 7?”

  1. Mantelinga Avatar
    Mantelinga

    This is working fine but not expected when user select value suggestion should be closed here it’s not.

  2. Ajay Malhotra Avatar

    Okay, I will check and update on it and thank you for notice.

  3. Julien Pitt Avatar
    Julien Pitt

    Great and it worked for me.

  4. saeed Avatar
    saeed

    form validation and (change) is not working on it.

  5. Ajay Malhotra Avatar

    Can you please share that code, how are you doing?

  6. Estevan Gomez Avatar
    Estevan Gomez

    Thanks bro, u helped me a lot

  7. Ajay Malhotra Avatar
    Ajay Malhotra

    You are welcome

  8. italo Avatar
    italo

    Thanks man! 2 days i searched for this solution kkkkk

  9. Geraldo Avatar
    Geraldo

    Hi,

    This code:

    {{item.descricao}}

    When a use a simple select, binds work, when I use select 2 do nothing.

    Can you help me.

  10. Ajay Malhotra Avatar

    I will show you dynamic working example.

  11. Muhammad Behroz Avatar
    Muhammad Behroz

    (change) is not working .. if it start working my problems will be solved…So kindly provide me some help.

  12. Ajay Malhotra Avatar

    Can you please explain it properly?

  13. Muhammad Behroz Avatar
    Muhammad Behroz

    when the drop down is appears it show values… but when we select any value it does not work .. i mean to say selection is not working… on selection i want my function to execute.. . i am also sharing my code..

    {{customer.Name}}

    in first line of code i am using (change)=”function()”..
    kindly help as soon as you can i will be very thankful to you..

  14. Muhammad Behroz Avatar
    Muhammad Behroz

    Code is here.. in above comment something went wrong ..
    kindly see this code

    {{customer.Name}}

  15. Ajay Malhotra Avatar
    Ajay Malhotra

    I got it, you want to get and show selected value?

  16. Muhammad Behroz Avatar
    Muhammad Behroz

    Yes . i want to get selected value. and on selection of value i want to execute my function that is written in typescript file

  17. Jamal Haider Avatar
    Jamal Haider

    Well done sir

  18. Ajay Malhotra Avatar
    Ajay Malhotra

    OKay fine

  19. saeed Avatar
    saeed

    do you solve your problem of (change)=”function()”, i am unable to get the change function.

  20. saeed Avatar
    saeed

    sir please look at the code

    {{option.name}}

    changeofCountry() is not calling

  21. Rk Avatar
    Rk

    how to get change event in select2

  22. Ajay Malhotra Avatar

    I will update it.

  23. Dhaval Avatar
    Dhaval

    Getting Error on compile time
    jquery(‘.js-example-basic-single’).select2();
    Property ‘select2’ does not exist on type ‘JQuery’. Did you mean ‘select’?

  24. Ajay Malhotra Avatar

    Did you add jquery?

  25. Jakob Hesse Avatar
    Jakob Hesse

    plz how to get the selected value without jquery methods

  26. Ajay Malhotra Avatar

    Will update you soon.

  27. Waqas Avatar
    Waqas

    This issue is in angular 9

  28. Waqas Avatar
    Waqas

    It’s not working in angular 9
    Please help

  29. Waqas Avatar
    Waqas

    its working fine now thanks but how i get change event value change event is not triggered simple select its working but select 2 its not working

  30. Ajay Malhotra Avatar

    I will also make example on it.

  31. Waqas Avatar
    Waqas

    Thanks dost I need it on urgent basis

  32. Ajay Malhotra Avatar

    I will update you, thanks

  33. Waaas Avatar
    Waaas

    Thanks bro thank you so much please if u don’t mind share a whts up number

  34. vicky Avatar
    vicky

    ngAfterViewInit()
    {
    console.log(this.tagLesson.nativeElement);
    this.tagLesson.nativeElement.select2();
    } this is what i written in my ts file. in console it shows the the element of select class but cant apply select 2.
    .select2 is not a function, this is what coming after the steps i followed, does anybody know the reason?.