Setting initial values of Angular-UI Select2 multiple directive
我有一个select2指令,用于具有自定义查询的多个选择国家/地区来抓取数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // Directive <input ng-model="filters.countries" ui-select2="filters.countryOptions" data-placeholder="Choose a country..."> // filters.countryOptions { multiple: true, query: function() { get_list_of_countries(); } } // Formatted data from remote source [ {id: 'US', text: 'United States'}, {id: 'CA', text: 'Canada'} ... ] |
我正在尝试使用以下方法在控制器中设置初始选择的值:
1 | $scope.filters.countries = [{id: 'US', text: 'United States'}]; |
这样可以正确设置模型,但是这是在select2初始化发生之前发生的。当我逐步执行其余的初始化代码时,输??入会暂时显示
为了解决这个问题,我正在使用以下方法重置模型的初始值:
1 2 3 4 5 | $scope.$on('$viewContentLoaded', function() { setTimeout(function() { $scope.filters.countries = [{id: 'US', text: 'United States'}]; }, 100); }); |
使用
更新1
根据ProLoser的要求,这里有一个演示和github票。
演示:http://plnkr.co/edit/DgpGyegQxVm7zH1dZIJZ?p = preview
GitHub问题:https://github.com/angular-ui/angular-ui/issues/455
遵循ProLoser的建议,我开始使用select2的initSelection函数:
1 2 3 | initSelection : function (element, callback) { callback($(element).data('$ngModelController').$modelValue); }, |
它可以解决问题,但仍感觉是一种解决方法。
根据ProLoser的要求,这里有一个演示和github票。
演示:http://plnkr.co/edit/DgpGyegQxVm7zH1dZIJZ?p = preview
GitHub问题:https://github.com/angular-ui/angular-ui/issues/455
遵循ProLoser的建议,我开始使用select2的initSelection函数:
1 2 3 | initSelection : function (element, callback) { callback($(element).data('$ngModelController').$modelValue); }, |
它可以解决问题,但仍感觉是一种解决方法。
您是否尝试过将选项初始化为:
1 | <option selected value="0">Name</option> |