简体   繁体   English

Angular-UI select2动态更改选项中的标签属性

[英]Angular-UI select2 dynamically change tags attribute in options

Official example for Angular-ui select2 tags is: Angular-ui select2标签的官方示例是:

myAppModule.controller('MyController', function($scope) {
    $scope.list_of_string = ['tag1', 'tag2']
    $scope.select2Options = {
        'multiple': true,
        'simple_tags': true,
        'tags': ['tag1', 'tag2', 'tag3', 'tag4']  // Can be empty list.
    };
});

And I have this piece of code: 我有这段代码:

$scope.select2Options = {
                        'multiple': true,
                        'simple_tags': true,
                        'tags': $scope.categoryNames
                    };

$scope.$watch(function () { return adminCrudService.getCategoriesForUpdate(); }, function () {
                $scope.action = "edit";
                $scope.categoriesForUpdate = adminCrudService.getCategoriesForUpdate();
                if ($scope.categoriesForUpdate.length > null) {
                    $scope.categoryNames = [];
                    _.forEach($scope.categoriesForUpdate, function (item) {
                        $scope.categoryNames.push(item._backingStore.Name);
                    });

                }
            });

But this just doesn't work, when I click on Selcet2, I get No matches found and I've logged $scope.categoryNames inside $watch, and data is there (so that part works fine). 但这是行不通的,当我单击Selcet2时, 没有找到匹配项,并且在$ watch中记录了$ scope.categoryNames ,并且有数据(因此该部分工作正常)。

So my question is can tags be loaded dynamically, and if they can, how ? 所以我的问题是标签可以动态加载吗?如果可以,如何加载?

I've recently encountered this issue when using the AngularUI Select2 project, and solved it by making the tags argument a function that returns the model. 我最近在使用AngularUI Select2项目时遇到了此问题,并通过使tags参数成为返回模型的函数来解决了该问题。 For example: 例如:

$scope.select2Options = {
  multiple: true,
  simple_tags: true,
  tags: function () {
    return $scope.categoryNames;
  }
};

Any updates to the $scope.categoryNames is reflected in the view because Select2 calls the tags function when opened and on every key press. $scope.categoryNames任何更新$scope.categoryNames反映在视图中,因为Select2在打开时以及每次按键时都会调用tags功能。

Hopefully this is useful for people wanting to use Select2 rather than Chosen. 希望这对希望使用Select2而不是选择的用户有用。

好吧,我无法使它正常工作,所以我遵循这个很棒的教程: link ,选择使用Chosen,我很快就得到了想要的结果。

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

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