简体   繁体   English

无法使bootstrap multiselect与角工作

[英]Can't get bootstrap multiselect to work with angular

I'm trying to get the bootstrap multiselect widget to work. 我正在尝试使bootstrap multiselect小部件正常工作。 It works when I hardcode all the options, like this: 当我对所有选项进行硬编码时,它会起作用,如下所示:

<select id="topic-select" multiple="multiple">
  <option val='math'>math</option>
  <option val='critical_reading'>critical reading</option>
  <option val='writing'>writing</option>
</select>



$("#topic-select").multiselect({
  includeSelectAllOption: true,
  selectAllText: 'composite score',
  allSelectedText: 'composite score',
  selectAllNumber: false,
});

but if I try to populate the options with angular, like this: 但是如果我尝试用angular填充选项,像这样:

<select id="topic-select" multiple="multiple" ng-option="topic in topicList">
 </select>

then the dropdown window kindof bugs out, and doesn't show any of the options. 那么下拉窗口就会出现错误,并且不会显示任何选项。

If I remove the javascript turning it into a multiselect, then it DOES show all the options. 如果我删除将其变成多选的JavaScript,那么它会显示所有选项。

I took a look at this similar question: angularjs-ng-repeat-in-bootstrap-multiselect-dropdown but couldn't didn't have any luck with it. 我看了一个类似的问题: angularjs-ng-repeat-in-bootstrap-multiselect-dropdown,但没有运气。

you don't really require Bootstrap multi-select if you're going for its functionality. 如果您要使用Bootstrap的功能,则实际上并不需要多选。 You can get the same functionality in Angular, by populating your options in a dropdown, and adding them to a new list on ng-click. 通过在下拉列表中填充选项并将其添加到ng-click的新列表中,可以在Angular中获得相同的功能。

    <span uib-dropdown on-toggle="toggled(open)" auto-close = "outsideClick" >
     <a class = "filter-names" href id="simple-dropdown" uib-dropdown-toggle>
       My_Dropdown<span ng-repeat="list in generated_list">{{list.genre_name}},</span><b class="caret"></b>
     </a>
     <ul class="dropdown-menu" uib-dropdown-menu aria-labelledby="simple-dropdown" >
        Search: <input type = "text" placeholder = "search in my list" ng-model = "search.name" />
        <li ng-repeat="item in list_to_populate | filter:search" ng-class = "{true: 'filter-selected', false: ''}[item.selected == true]" ng-click="addToFilter(item)">
            {{item.name}}
        </li>
     </ul>
</span>

And in the controller: 并在控制器中:

    $scope.addToFilter = function(item) {

  if(item.selected == "undefined" || item.selected == false)
  {
    item.selected = true;
    Filters.addToFilters(item);
  }
  else
  {
    Filters.removeFromFilters(item);
    item.selected = false;
  }
}

And finally have a service "Filters" to store this list and call functions to use it anywhere. 最后,有一个“过滤器”服务来存储此列表并调用函数以在任何地方使用它。

  1. You are missing "ng-model". 您缺少“ ng-model”。
  2. It is "ng-options" and not "ng-option". 它是“ ng-options”而不是“ ng-option”。

Try this: 尝试这个:

<select id="topic-select" multiple ng-model="selectedTopics" ng-options="topic as topic.name for topic in topicList">
</select>

Instead of populating the options with angular, I just add them to the div with vanilla javascript like this: 而不是用angular填充选项,我只是使用香草javascript将它们添加到div中,如下所示:

var topicSelect = $("#topic-select");
for (var topicId in topicList) {
  topicSelect[0].add(new Option(topicList[topicId], topicId));
}

and everything works now. 现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM