【问题标题】:Bootstrap-select,angularjs dropdown not workingBootstrap-select,angularjs下拉菜单不起作用
【发布时间】:2014-11-16 06:15:48
【问题描述】:

我有这个问题,希望得到一些关于如何解决它的建议。

我已将部分 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>   

提前谢谢你。

【问题讨论】:

    标签: angularjs dropdownbox bootstrap-select


    【解决方案1】:

    我有另一个解决相关问题的方法。

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

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

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

    【讨论】:

      【解决方案2】:

      混合使用 Angular + jQuery 不是一个好习惯。如果你想使用任何 jQuery 插件,你可以将它包装成一个自定义的 angular 指令 (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 作为链接函数的第四个参数(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:

      您已选择:{{ 已选择 | 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>
      

      【讨论】:

      • 您认为在指令中使用 jquery 还是完全不使用它们更容易接受?我遇到的问题是我的代码运行良好,但是当我将代码放在部分视图中时它不起作用。我正在使用 ng-view 和具有 multiselct 下拉列表的部分 html 文件。它看起来完全一样,但是当我按下下拉菜单时,没有任何反应,它没有打开。我也尝试更改库的顺序,但没有帮助。我相信我的问题与我在局部视图中使用某些东西有关,尽管所有引导程序、角度库都正在加载。
      • 是的,在指令中使用 jQuery 是可以接受的。您要避免的是使用 jQuery 选择元素。指令为您提供(参见示例)。请参阅我的编辑答案以绑定到控制器
      • 问题是下拉菜单只有在你有静态值时才有效。我已经更改了您的示例,因此它使用了 ng-options。现在你会注意到它不起作用jsfiddle.net/7hree57z/3
      • 您最初的问题中没有对ngOptions 的引用。也许更新它或创建一个新的?
      • 我在之前的评论中发布的 jsfiddle 是我现在的代码设置方式;我已经尝试过 ng-repeat(最初)和 ng-options。两者都给出了同样的问题。如果您检查以下示例,您会注意到您无法打开下拉列表jsfiddle.net/7hree57z/3
      猜你喜欢
      • 2013-01-21
      • 2012-03-20
      • 2021-12-19
      • 2020-07-13
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多