2

I am trying to use bootstrap selectpicker in my angular2 application. I have loaded jquery and bootstrap-select scripts in angular-cli.json.

This is my app-component.ts

 import $ from 'jquery';
  ngOnInit() {
   $('.selectpicker').selectpicker('refresh');
  }

But, I am getting this error.

__WEBPACK_IMPORTED_MODULE_2_jquery___default(...)(...).selectpicker is not a function.

I am new to angular2. Please help!

3
  • you still requires the bootstrap.js for bootstrap's selectpicker Commented Aug 1, 2017 at 4:00
  • "styles": [ "../node_modules/bootstrap/dist/css/bootstrap.min.css", "../node_modules/font-awesome/css/font-awesome.min.css", "../node_modules/typeface-montserrat/index.css", "../node_modules/bootstrap-select/dist/css/bootstrap-select.min.css", "styles.css" ], "scripts": ["../node_modules/jquery/dist/jquery.min.js", "../node_modules/bootstrap/dist/js/bootstrap.min.js", "../node_modules/bootstrap-select/dist/js/bootstrap-select.min.js" ] Commented Aug 1, 2017 at 4:10
  • I have inluded all these files in angular-cli.json Commented Aug 1, 2017 at 4:12

1 Answer 1

4

Here is my Solution,

Open typings.d.ts file in angular-cli and add the following lines

interface JQuery {
   addClass(className: string): JQuery;
   attr(attributeName: string, value: string|number): JQuery;
}
interface JQuery {
     selectpicker(options?: any, callback?: Function):any;
}

Don't import jquery instead:

declare var $:any;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This worked. But could you please explain what actually is happening here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.