gpt4 book ai didi

angularjs - 设置 Angular-UI Select2 多重指令的初始值

转载 作者:行者123 更新时间:2023-12-02 21:55:01 24 4
gpt4 key购买 nike

我有一个 select2 指令,用于多个国家/地区的选择,并使用自定义查询来获取数据:

// 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'} ...
]

我正在尝试使用以下方法在 Controller 中设置最初选择的值:

$scope.filters.countries = [{id: 'US', text: 'United States'}];

这确实正确设置了模型,但是这是在 select2 初始化发生之前发生的。当我单步执行剩余的初始化代码时,输​​入暂时显示 [Object],然后最终清空 $scope.filters.countries 和输入,但它不显示占位符输入中的文本。

为了解决这个问题,我使用以下方法来重置模型的初始值:

$scope.$on('$viewContentLoaded', function() {
setTimeout(function() {
$scope.filters.countries = [{id: 'US', text: 'United States'}];
}, 100);
});

使用setTimeout看起来确实很黑客。我缺少更好的方法吗?

更新 1

根据 ProLoser 的要求,这里有一个演示和 github 票证。

演示:http://plnkr.co/edit/DgpGyegQxVm7zH1dZIJZ?p=preview

GitHub 问题:https://github.com/angular-ui/angular-ui/issues/455

按照 ProLoser 的建议,我开始使用 select2 的 initSelection 函数:

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 函数:

initSelection : function (element, callback) {
callback($(element).data('$ngModelController').$modelValue);
},

它确实有效,但仍然感觉像是一种解决方法。

关于angularjs - 设置 Angular-UI Select2 多重指令的初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14985346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com