I need to dynamically make an input field required (or not) and would like to set this value in a directive. I know there is the ng-required directive and can be set as ng-required="datamodel.required" but ultimately, I want to pull a settings object in the directive and turn on/off the required parameter based on that config. The config is in a service and I don't want to inject the service for each of my form controller - hence why I am needing to set this up in a directive.
Here is a JSFiddle starting point: http://jsfiddle.net/hm2b771z/2/
app.directive('requiredConfig', function($compile){
return {
link: function(scope, element, attrs){
console.log(attrs.requiredConfig);
console.log(attrs.ngRequired);
attrs.ngRequired = (attrs.requiredConfig == "true") ? true : false;
$compile( element.contents() )( scope );
console.log(attrs.ngRequired);
console.log('_______________________');
}
}
});
What I expect is the field that is the first field option to be required while the second one remains optional.
Thanks!
Instead of
Use