I have created an optionsAttribute
directive that can transform the attributes of your option tags after they are rendered by ngOptions
.
DEMO
Directive
.directive('optionsAttribute', function($parse, $timeout) {
return {
restrict: 'A',
compile: function(tElem, tAttr) {
var transform = $parse(tAttr.optionsAttributeTransform);
return function(scope, elem, attr) {
scope.$watch(attr.optionsAttribute, function(data) {
var options = elem.children();
options = Array.prototype.slice.call(options, options.length - data.length);
angular.forEach(options, function(option, index) {
var optionElement = angular.element(option);
var newAttribute;
var label;
if(angular.isDefined(attr.optionsAttributeTransform)) {
newAttribute = transform(scope, {
$data: data[index],
$label: option.innerHTML,
$index: index
});
angular.forEach(newAttribute, function(value, attribute) {
optionElement.attr(attribute, value);
});
}
});
});
};
}
};
});
Usage
- Provide the array value you have provided in the
ngOptions
directive in the optionsAttribute
directive.
e.g.
<select ng-options="font for font in reportFontload"
options-attribute="reportFontload">
</select>
- Add a callback in the
optionsAttributeTransform
directive that returns an json object that represents the attributes to be appended in each of the option
s tag. The callback itself is provided with 2 values:
$data - represents each of the item in the array provided in #1.
$index - represents the index of the $data
.
e.g.
HTML
<select ng-options="font for font in reportFontload"
ng-model="selected"
options-attribute="reportFontload"
options-attribute-transform="{ 'style': 'font-family: ' + $data }">
</select>
or an alternative is to provide a function callback
JAVASCRIPT
$scope.transform = function(fontFamily) {
return { 'style': 'font-family ' + fontFamily };
};
HTML
<select ng-options="font for font in reportFontload"
ng-model="selected"
options-attribute="reportFontload"
options-attribute-transform="transform($data)">
</select>
LIMITATIONS
The directive is only limited towards array, objects excluded, although there is a way to tweak the current directive to also work with objects, but the current implementation is already sufficient for the OP's problem.
UPDATE
I tweaked the directive above to also work with object collections, the usage still says the same:
DEMO
(function(ng) {
'use strict';
var app = ng.module('options.attribute', []);
app.directive('optionsAttribute', function($parse) {
return {
restrict: 'A',
compile: function(tElem, tAttr) {
var transform = $parse(tAttr.optionsAttributeTransform);
return function(scope, elem, attr) {
scope.$watch(attr.optionsAttribute, function(collection) {
var options = elem.children();
collection = new Collection(collection);
options = Array.prototype.slice.call(options, options.length - collection.getLength());
ng.forEach(options, function(option, index) {
var newAttributes = transform(scope, {
$data: collection.getItem(index),
$label: option.innerHTML,
$index: collection.getIndex(index)
});
var optionElement = angular.element(option);
ng.forEach(newAttributes, function(value, attribute) {
optionElement.attr(attribute, value);
});
});
}, true);
};
}
};
});
function Collection(collection) {
var self = this;
var indexes;
if(angular.isObject(collection)) {
if(Object.keys) {
indexes = Object.keys(collection);
} else {
ng.forEach(collection, function(item, index) {
indexes.push(index);
});
}
self.getItem = function(index) {
return collection[indexes[index]];
};
self.getLength = function() {
return indexes.length;
};
self.getIndex = function(index) {
return indexes[index];
};
} else if(angular.isArray(collection)) {
self.getItem = function(index) {
return collection[index];
};
self.getLength = function() {
return collection.length;
};
self.getIndex = function(index) {
return index;
};
}
}
})(angular);