I have been cracking my head on this particular issue and I just can't seem to find the solution for it.
Basically, what I'm trying to achieve is to have a select list (select[multiple]) and bind an array to it with values. But as the list grows, I'm adding a filter to it so I don't have to scroll and search individually.
But by doing so, my model (array) is being cleared or more likely, all the values that are stored in there are being dropped except those that are results of the filtering.
Anyone have any idea how to tackle this particular issue?
To simulate the issue, select multiple items and filter on one item in particular. Clear the filter and the model is cleared.
Directive code
angular
.module("multiselect", [])
.directive("multiselect", [function() {
return {
template: '<div>' +
' <input type="text" data-ng-model="filter" placeholder="filter"/>' +
' <select multiple="multiple" data-ng-model="selection" data-ng-options="item.value as item.label for item in items | filter: filter"/>' +
'</div>',
link: function(scope, elem, attr) {
scope.selection = [];
scope.items = [
{ label: 'Item 1', value: 1 },
{ label: 'Item 2', value: 2 },
{ label: 'Item 3', value: 3 },
{ label: 'Item 4', value: 4 },
{ label: 'Item 5', value: 5 },
];
}
}
}]);
HTML
<div ng-app="multiselect">
<multiselect></multiselect>
</div>
Example > fiddle