Bootstrap-select,angularjs下拉列表无法正常工作

时间:2014-09-22 10:01:55

标签: angularjs dropdownbox bootstrap-select

我有这个问题,希望能就如何修复它提出一些建议。

我已将部分html页面移动到局部视图中并通过ng-view加载 现在的问题是,下拉菜单选择了不再工作了,我设法恢复了样式,但是当我点击它时,它没有打开。 如果它不在局部视图中,我甚至不需要进行javascript调用,这似乎是现在需要的。

这是我的代码

index.html
 <link href="resources/css/bootstrap.min.css" rel="stylesheet">
    <link href="resources/css/bootstrap-select.css" rel="stylesheet">
    <!-- Bootstrap Core CSS -->

    <script src="resources/js/jquery-2.1.1.min.js"></script>
    <script src="resources/js/jquery-1.11.0.js"></script>

    <script src="resources/js/bootstrap-select.js"></script>

    <script src="resources/library/angular.js"></script>
    <script src="resources/library/angular-route.js"></script>
    <script src="resources/js/MainController.js"></script>
    <script src="resources/js/main-app.js"></script>

    partialview.html
    -------------------
     <select class="selectpicker" multiple ng-model="selE">
                    <option ng-repeat="e in ee">{{e}}</option>
                </select>

    <script>
    $(document).ready(function () {

        $('select').selectpicker({
            style: 'btn-default',
            size: false
        });

    });

</script>   

提前谢谢你。

2 个答案:

答案 0 :(得分:7)

混合Angular + jQuery不是一个好习惯。如果你想使用任何jQuery插件,你可以将它包装成一个自定义角度指令(official docs)。

在此 jsFiddle 中查看selectpicker指令的工作示例。

html:

<select class="selectpicker"
        multiple
        title='Choose one of the following...'>
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>

js:

angular.module('demo')
  .directive('selectpicker', function () {
    return {
      restrict: 'C',
      link: function (scope, element) {
        $(element).selectpicker({
          style: 'btn-default',
          size: false
        });
      }
    };
  });

链接到控制器

在指令中使用require: 'ngModel'会将ngModelController作为第4个参数来链接函数(see doc here)。

因此,您可以轻松地将控制器值与指令范围绑定。

请在此处查看 other jsFiddle

指令:

angular.module('demo')
  .directive('selectpicker', function () {
    return {
      restrict: 'C',
      require: 'ngModel',
      link: function (scope, element, attrs, ngModel) {
        var $el = $(element);
        $el.selectpicker({
          style: 'btn-default',
          size: false
        });
        $el.on('change', function (ee, aa) {
          ngModel.$setViewValue($el.val());
          scope.$apply();
        });
      }
    };
  });

控制器:

angular.module('demo')
  .controller('MainCtrl', function ($scope) {
    $scope.selected = [];
  });

HTML:

You have selected : {{ selected | json }}

<select ng-model="selected"
        class="selectpicker"
        multiple
        title='Choose one of the following...'>
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>

传递参数

中的选项

请参阅 3rd jsFiddle

要动态传递选项,只需更新指令:

angular.module('demo')
  .directive('selectpicker', function ($timeout) {
    return {
      restrict: 'C',
      require: 'ngModel',
      replace: true,
        template: '<select multiple><option ng-repeat="opt in options">{{ opt }}</option></select>',
      scope: {
        options: '='
      },
      link: function (scope, element, attrs, ngModel) {
        var $el = $(element);
        $timeout(function () {
          $el.selectpicker({
            style: 'btn-default',
            size: false
          });
        });
        $el.on('change', function (ee, aa) {
          ngModel.$setViewValue($el.val());
          scope.$apply();
        });
      }
    };
  });

然后在你的html:

<div ng-model="selected"
        class="selectpicker"
        options="issues"
        title='Choose one of the following...'>
</div>

答案 1 :(得分:1)

我得到了另一个相关问题的解决方案。

我所做的是首先使用angularjs从后端加载我的数据,并在验证后在.success内加载我编写以下代码:

angular.element(document).ready(function () { 
  $('select').selectpicker("destroy"); 
  $('select').selectpicker("render"); 
});

我试图在没有angular.element(document).ready(function (){})的情况下这样做,但什么也没发生。我认为插件的方法只适用于该函数。

相关问题