加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:18:42 所属栏目:安全 来源:网络整理
导读:我有一个select2指令用于多个选择的具有自定义查询的国家/地区以获取数据: // Directiveinput ng-model="filters.countries" ui-select2="filters.countryOptions" data-placeholder="Choose a country..."// filters.countryOptions{ multiple: true,query
我有一个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'} ...
]

我试图在我的控制器中设置最初选择的值:

$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 Issue: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 Issue:https://github.com/angular-ui/angular-ui/issues/455

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

initSelection : function (element,

它的诀窍,但仍然感觉像一个解决方法。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读