2

What is proper way to implement this bootstrap select plugin (http://silviomoreto.github.io/bootstrap-select/) to angular. I have put it in the directive:

App.directive('selectmultiple', [function(){
    return function(scope, element, attributes){
        element = $(element[0]);

        element.selectpicker({

        })
    }
})

But it's not working. If I write in Chrome console $('.selectpicker').selectpicker({}) - proper combobox appear (opened), but a can't close it. Thanks

2
  • if you have jQuery loaded BEFORE angular in page element is a jQuery object already so no need to do $(element[0]);. Create a demo in jsfiddle.net or plunker that replicates problem Commented Nov 7, 2013 at 21:11
  • If the directive has a template or uses ng-repeat to generate the child elements, then it will not be rendered till the next $digest loop. In that case, you may have to add a setTimeout or $timeout with interval 0 and execute element.selectpicker() in it to make sure the HTML exists before the selectpicker function is called. Commented Nov 7, 2013 at 22:11

1 Answer 1

4

I created a working example in jsfiddle. The way I found to use bootstrap-select plugin with angular was:

HTML:

 <div ng-app="myApp">
 <div ng-controller="MyCntrl">
     <select ng-model='color' multiple ng-multiple="true" ng-options='c.name for c in colors' select-multiple></select> 
 </div>
 </div>

Controller:

angular.module('myApp.controllers', [])
  .controller('MyCntrl', ['$scope', function($scope) {
        $scope.colors = [
                    {name:'black'},
                    {name:'white'},
                    {name:'red'},
                    {name:'blue'},
                    {name:'yellow'}
                  ];
        $scope.color = $scope.colors[2]; // red

  }]);

Directive:

angular.module('myApp.directives', [])
  .directive('selectMultiple', function() {
    return function(scope, element, attributes){
        element.selectpicker({});

        scope.$watch(function () {
          return element[0].length;
         }, function () {
          element.selectpicker('rebuild');
         });  
          // Watch for any changes from outside the directive and refresh
        scope.$watch(attributes.ngModel, function () {
          element.selectpicker('refresh');
        });
    }
  });

I hope it helps you ;)

Sign up to request clarification or add additional context in comments.

4 Comments

provide link to fiddle, please
It works in fiddle, but I didn't try it in project cause I've already implemented this plugin github.com/amitava82/angular-multiselect Thanks for your help.
I tried the solution but my ng-model is not being set. Do I need to do a ngModel.$render?
Having the same issue here. ngModel is not being set. Any solutions?

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.