简体   繁体   中英

AngularJs Select2 multiple: ng-selected does not work

I'm using the following piece of code to select item if it is present in filters

            <div ng-repeat="category in categories.data" ng-model="div1">
            <div ng-repeat="(key, value) in category" mg-model="div1.div2">
                {{ key + ":"}}
                <select id={{key}} class="my_select"
                        data-ng-model="CategoryOption"
                        data-ng-change="updateCategories()"
                        data-ui-select2="{}" multiple >
                    <option ng-repeat="c in value"
                            ng-selected="(filters[key].length>0) && (filters[key].indexOf(c.trim()) !== -1)" >
                        {{c.trim()}}</option>
                </select>

            </div>
        </div>

But it actually doesn't select anything... Other option is to set ng-model to filters.key, but selecting one element will cancel out the selection in another select, because they are bind to the same model...

Given my setup from above, how can I restore my selection, using select2 multiple ?

The only solution I found for my problem was to modify select2.js once again.

I used to pass a custom argument:

<select id={{key}} class="my_select"
                   ng-model="select2"
                   ui-select2="{mySelection: filters[key] }" multiple >
                   <option ng-repeat="c in value track by $index"
                       value="{{c.trim()}}">{{c.trim()}}</option>
</select>

and in select2 I applied following changes:

if(opts.mySelection && typeof opts.mySelection != undefined) {
    elm.val(opts.mySelection).trigger("change");
}

just before:

controller.$render();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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