I wrote a simple directive to be able to dynamically set the value of ng-model, as described in this other Stack Overflow question: AngularJS - bind ng-model to a variable which name is stored inside another variable. This works great, except when I use it on an element that also has an ng-repeat. It gets rendered too many times and the DOM does not look correct.
Directive:
angular.module( 'dynamicModel-directive', [] )
.directive( 'dynamicModel', function( $compile ) {
return {
link: function( scope, element, attr ){
element[0].removeAttribute( 'dynamic-model' );
element[0].setAttribute( 'ng-model', scope.$eval( attr.dynamicModel ) );
$compile( element[0] )( scope );
}
};
});
HTML (simplified by rendering an input box instead of a button group):
<div ng-if="field.type == 'buttonGroup'">
<input type="text" ng-repeat="option in field.options"
dynamic-model="field.name" class="form-control">
</div>
The above code results in four input boxes being displayed, even though there are only two items in field.options.
Here is what the resulting DOM looks like:
<div ng-if="field.type == 'buttonGroup'" class="ng-scope">
<!-- ngRepeat: option in field.options -->
<!-- ngRepeat: option in field.options -->
<input type="text" ng-repeat="option in field.options" class="form-control ng-scope ng-pristine ng-valid" ng-model="application.insured.gender">
<!-- end ngRepeat: option in field.options -->
<input type="text" ng-repeat="option in field.options" class="form-control ng-scope ng-pristine ng-valid" ng-model="application.insured.gender">
<!-- end ngRepeat: option in field.options -->
<!-- end ngRepeat: option in field.options -->
<!-- ngRepeat: option in field.options -->
<input type="text" ng-repeat="option in field.options" class="form-control ng-scope ng-pristine ng-valid" ng-model="application.insured.gender">
<!-- end ngRepeat: option in field.options -->
<input type="text" ng-repeat="option in field.options" class="form-control ng-scope ng-pristine ng-valid" ng-model="application.insured.gender">
<!-- end ngRepeat: option in field.options -->
<!-- end ngRepeat: option in field.options -->
When I comment out the $compile line in the directive it displays the appropriate number of times so I think it is related to that, but I am not sure how to fix it. Any ideas? Thanks!