I am rewriting this question as I think the original was not too clear.
Basically, I have a 'wrapper' directive where I am trying to dynamically add attributes to one of the transcluded elements. I can make this work, but Angular does not seem to be aware of the new attributes after adding.
If I use $compile , then Angular recognizes them, but by double compiling the translated content, in which case it doubles the number of options in the select tag.
Here is a plunker that shows (with comments) what I'm trying, and the same code follows below for those who can see the code and offer an answer just by looking: (note - my final goal is to check the custom valid-form-group for the required attribute, and if found to apply to the contained select tag)
HTML
<body ng-controller="MainCtrl"> <form name="validationForm" novalidate> <valid-form-group class="form-group" ng-class="{'has-error': validationForm.validInfo.$error.required}" required> <select ng-model="data.option" ng-options="option.id as option.message for option in selectOptions" name="validInfo" id="validInfo"> <option value="">-- Select a Question --</option> </select> </valid-form-group> </form> </body>
Js
var app = angular.module('plunker', []) .controller('MainCtrl', function($scope) { $scope.selectOptions = [ {id: 1, message: 'First option'}, {id: 2, message: 'Second option'}, {id: 3, message: 'Third option'} ]; }) .directive('validFormGroup', function($compile) { return { restrict: 'E', template: '<div><span ng-transclude></span></div>', replace: true, transclude: true, require: '^form', link: function(scope, element, attrs, ctrl) { if (attrs.required !== undefined) { var selectElement = angular.element(element.find('select'));
CSS
.has-error{ border: solid 1px red; }
Note that the example here uses " required " (or ng-required ) as an added attribute to emphasize the fact that Angular will not recognize it if it is not compiled.
Any help or comments are welcome - a little disappointed that I can't get this to work, so maybe there is something fundamental that I am missing ...
plunker should help in visualizing my problem.
edit - apologies for the delay in responding to replies and comments. As mentioned in a comment or two below, personal problems did not allow me to find the time to investigate.