inject select2 in typeScript

1.9k views Asked by At

I have problem injecting the select2 library in my TypeScript/Angular project. I don't know what else to do to make it working, I searched for some working example of but without success.

  • jQuery gets loaded

  • select2.js gets loaded

  • definition file select2.d.ts is there (I also have matching version)

Module is injected like that:

public init() {
        var myApp = angular.module('app', [
            'ui.router',
            'ngCookies',
            'ui.bootstrap',
            'select2'
        ]); 
...

I get an error:

Module 'select2' is not available! You either misspelled the module name or forgot to load it.

What am I doing wrong there? Any help appreciated.

2

There are 2 answers

0
quicky On BEST ANSWER

So I resolved this by initializing select2 in jQuery style direct in my controller...

$('select').select2();

It is probably not very nice solution but it works. If anyone got better solution feel free to post it

1
Kevin Brown-Silva On

You are trying to inject Select2 into your application like an Angular module. This is throwing an error because Select2 is not an angular module, it's a jQuery component.

Angular UI provides ui-select which is an Angular native version of Select2.