无法使 bootstrap 多选与 angular 一起使用
Can't get bootstrap multiselect to work with angular
我正在尝试让 bootstrap 多选小部件正常工作。当我对所有选项进行硬编码时它会起作用,如下所示:
<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,
});
但是如果我尝试用 angular 填充选项,就像这样:
<select id="topic-select" multiple="multiple" ng-option="topic in topicList">
</select>
然后下拉菜单 window 有点出问题,并且不显示任何选项。
如果我删除 javascript 将其变成多选,那么它确实会显示所有选项。
我看了这个类似的问题:
angularjs-ng-repeat-in-bootstrap-multiselect-dropdown
但不能没有任何运气。
- 你不见了 "ng-model".
- 它是 "ng-options" 而不是 "ng-option"。
试试这个:
<select id="topic-select" multiple ng-model="selectedTopics" ng-options="topic as topic.name for topic in topicList">
</select>
我没有使用 angular 填充选项,而是将它们添加到 div with vanilla javascript 中,如下所示:
var topicSelect = $("#topic-select");
for (var topicId in topicList) {
topicSelect[0].add(new Option(topicList[topicId], topicId));
}
现在一切正常。
如果您要使用它的功能,您实际上并不需要 Bootstrap multi-select。您可以在 Angular 中获得相同的功能,方法是在下拉列表中填充您的选项,然后将它们添加到 ng-click 的新列表中。
<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>
在控制器中:
$scope.addToFilter = function(item) {
if(item.selected == "undefined" || item.selected == false)
{
item.selected = true;
Filters.addToFilters(item);
}
else
{
Filters.removeFromFilters(item);
item.selected = false;
}
}
终于有了一个服务 "Filters" 来存储这个列表并调用函数以在任何地方使用它。
我正在尝试让 bootstrap 多选小部件正常工作。当我对所有选项进行硬编码时它会起作用,如下所示:
<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,
});
但是如果我尝试用 angular 填充选项,就像这样:
<select id="topic-select" multiple="multiple" ng-option="topic in topicList">
</select>
然后下拉菜单 window 有点出问题,并且不显示任何选项。
如果我删除 javascript 将其变成多选,那么它确实会显示所有选项。
我看了这个类似的问题: angularjs-ng-repeat-in-bootstrap-multiselect-dropdown 但不能没有任何运气。
- 你不见了 "ng-model".
- 它是 "ng-options" 而不是 "ng-option"。
试试这个:
<select id="topic-select" multiple ng-model="selectedTopics" ng-options="topic as topic.name for topic in topicList">
</select>
我没有使用 angular 填充选项,而是将它们添加到 div with vanilla javascript 中,如下所示:
var topicSelect = $("#topic-select");
for (var topicId in topicList) {
topicSelect[0].add(new Option(topicList[topicId], topicId));
}
现在一切正常。
如果您要使用它的功能,您实际上并不需要 Bootstrap multi-select。您可以在 Angular 中获得相同的功能,方法是在下拉列表中填充您的选项,然后将它们添加到 ng-click 的新列表中。
<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>
在控制器中:
$scope.addToFilter = function(item) {
if(item.selected == "undefined" || item.selected == false)
{
item.selected = true;
Filters.addToFilters(item);
}
else
{
Filters.removeFromFilters(item);
item.selected = false;
}
}
终于有了一个服务 "Filters" 来存储这个列表并调用函数以在任何地方使用它。