How can I use Bootstrap Multiselect Dropdown in AngularJS

后端 未结 3 666
礼貌的吻别
礼貌的吻别 2020-12-15 18:31

I want to use Bootstrap Multiselect Dropdown http://davidstutz.github.io/bootstrap-multiselect/ in AngularJS. I hear that it\'s necessary to move it to Directive. But I thin

3条回答
  •  粉色の甜心
    2020-12-15 19:10

    Here is a directive I use in my project. It works on Chrome and Firefox. You can change the options based on your own need.

    angular.module('yourApp')
    .directive('yourDirective', function () {
        return {
            link: function (scope, element, attrs) {
                element.multiselect({
                    buttonClass: 'btn',
                    buttonWidth: 'auto',
                    buttonContainer: '
    ', maxHeight: false, buttonText: function(options) { if (options.length == 0) { return 'None selected '; } else if (options.length > 3) { return options.length + ' selected '; } else { var selected = ''; options.each(function() { selected += $(this).text() + ', '; }); return selected.substr(0, selected.length -2) + ' '; } } }); // Watch for any changes to the length of our select element scope.$watch(function () { return element[0].length; }, function () { element.multiselect('rebuild'); }); // Watch for any changes from outside the directive and refresh scope.$watch(attrs.ngModel, function () { element.multiselect('refresh'); }); } }; });

    Update two snippets for the directive which working on AngularJS v1.3.15 and bootstrap-multiselect v0.9.6: JavaScript, CoffeeScript

提交回复
热议问题