Here is a custom directive that allows you to add additional attributes when using ng-options
with <select>
, eliminating the need for ng-repeat
.directive('optionsCustomAttr', function ($parse) {
return {
priority: 0,
require: 'ngModel',
link: function (scope, iElement, iAttrs) {
scope.addCustomAttr = function (attr, element, data, fnDisableIfTrue) {
$("option", element).each(function (i, e) {
var locals = {};
locals[attr] = data[i];
$(e).attr(iAttrs.customAttrName ? iAttrs.customAttrName : 'custom-attr', fnDisableIfTrue(scope, locals));
});
};
var expElements = iAttrs['optionsCustomAttr'].match(/(.+)\s+for\s+(.+)\s+in\s+(.+)/);
var attrToWatch = expElements[3];
var fnDisableIfTrue = $parse(expElements[1]);
scope.$watch(attrToWatch, function (newValue) {
if (newValue)
scope.addCustomAttr(expElements[2], iElement, newValue, fnDisableIfTrue);
});
}
};
})
In your HTML select element,
<select ng-model="selectedExportType" ng-options="option.value as option.title for option in exportTypes"
options-custom-attr="option.generatesFile for option in exportTypes"
custom-attr-name="data-generates-file">
</select>
Note: This directive is based on the concept of optionsDisabled
mentioned in this thread