For my project in Angular 1.4, I am utilizing the ngOptions
directive to dynamically fill a <select>
element with <option>
elements based on an object structure like this:
{black: '#000000', red: '#FF0000', ...}
The implementation works smoothly with
ng-options="value as key for (key, value) in vm.colors"
. However, I now want to assign a class to each option
element corresponding to its key (e.g. '.black', '.red'). I initially thought of targeting the option
elements and applying a class using addClass()
, but I'm encountering difficulties in selecting those elements. (Please note that I am not using jQuery and prefer not to include it just for this task.)
Here is a link to a related jsfiddle.
I assumed I could bind the results of $element.find('option')
to my view model and then monitor it using
$scope.$watch('vm.options', function() {...})
. However, when I inspect vm.options
in the console, I only see an empty array returned.
Could I be misusing $scope.$watch
in this context? Is the issue related to $element
? Are the elements nested within the scope of ngOptions
not accessible from my controller? Or am I overlooking a simple mistake?
Any assistance on this matter would be greatly valued...thank you in advance!