This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Extending select directive #13283
Closed
Description
I want to write directive that will check for available options in the scope and preselect some option if only one item present inside ngOptions.
For now I wrote something like this:
<select id="provider" name="provider" class="form-control"
ng-model="foo.provider"
ng-options="provider.name for provider in providers track by provider.id"
select-first-if-only-one="providers"
required>
<option value="">- Select -</option>
</select>
and my directive:
'use strict';
angular.module('app')
.directive('selectFirstIfOnlyOne', function() {
return {
restrict: 'A',
require: 'select',
link: function(scope, elem, attrs, ctrl) {
scope.$watchCollection(attrs.selectFirstIfOnlyOne, function(values) {
if (angular.isDefined(values) && values.length === 1) {
scope.$evalAsync(function() {
ctrl.ngModelCtrl.$setViewValue(values[0]);
});
}
});
}
};
});
And it works. But I want not to pass array values to directive directly but to take them from ngModel or ngOptions.
I found that SelectController
doesn't provide methods to get all values from <select>
, same for NgModelController
.
Same question on StackOverflow.
Metadata
Metadata
Assignees
Labels
No labels