I am having trouble toggling ng-selected options in Angular. I have tried the following approach:
<select ng-model="datacut.ages" multiple>
<option value="" disabled="disabled">Please Select</option>
<option value="0-15" ng-click="toggleSelect(datacut, '0-15')" ng-selected="datacut.ages.indexOf('0-15') !== -1">15 and Younger</option>
<option value="16-19" ng-selected="datacut.ages.indexOf('16-19') !== -1">16 - 19</option>
<option value="20-24" ng-selected="datacut.ages.indexOf('20-24') !== -1">20 - 24</option>
</select>
Controller:
$scope.toggleSelect = function(dc, str){
dc.ages.splice(dc.ages.indexOf(str), 1);
//e.currentTarget.selected = !e.currentTarget.selected;
};
The issue I'm facing is that when I click on an option, it gets selected on mousedown, but unselected on release. The commented out code behaves the same way.
I believe there should be a simple solution to this problem, but I haven't been able to find an elegant one.
Any assistance with this matter would be greatly appreciated.
Edit: Just to clarify - I need to have the option of having no elements selected, so clicking on a single selected element should unselect it. Additionally, I need to be able to select multiple options concurrently.