AngularJS - Silvio Moreto Bootstrap Select

by lu4x

HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script src="http://silviomoreto.github.io/bootstrap-select/javascripts/bootstrap-select.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css">
<link rel="stylesheet" href="http://silviomoreto.github.io/bootstrap-select/stylesheets/bootstrap-select.css">
<div ng-app="myApp">
    <div ng-controller="MyCtrl">
        <selectpicker data-array="items" data-model="myModel"></selectpicker>{{myModel}}
    </div>
</div>

JavaScript

var app = angular.module('myApp', []);

app.controller('MyCtrl', ['$scope', function($scope)
{
    $scope.items = [{value:1, option:'Option 1'},
                    {value:2, option:'Option2'}];
}]);

app.directive('selectpicker', function($timeout)
{
  return {
      restrict: 'E',
      scope: {
          array: '=',
          model: '=',
          class: '='
      },
      template: '<select class="selectpicker" ng-model="model" ng-options="o.value as o.option for o in array"></select>',
      replace: true,
      link: function(scope, element, attrs) {
          var el = angular.element(element);
          
          $timeout(function () {
              el.selectpicker();
          }, 0)
      }
  }
});