Skip to content

Instantly share code, notes, and snippets.

@aruss
Created December 3, 2014 09:00
Show Gist options
  • Save aruss/01fba84941f158ac798e to your computer and use it in GitHub Desktop.
Save aruss/01fba84941f158ac798e to your computer and use it in GitHub Desktop.
Dynamicaly add attributes, or a directive that can add directives, yo dawg!
app.directive('uiDynamicAttr', function($compile, $parse) {
return {
restrict: 'A',
terminal: true,
priority: 100000,
link: function(scope, element, args) {
var expression = element.attr('ui-dynamic-attr');
if (expression) {
var attributes = $parse(element.attr('ui-dynamic-attr'))(scope);
if (attributes) {
for (var i = 0; i < attributes.length; i++) {
var attribute = attributes[i];
if (angular.isObject(attribute)) {
element.attr(attribute.name, attribute.value | '');
}
else {
element.attr(attribute, '');
}
};
element.removeAttr('ui-dynamic-attr');
$compile(element)(scope);
}
}
}
};
});
@aruss
Copy link
Author

aruss commented Sep 7, 2015

field.validation = [
{
name: 'required',
message: 'Value is required.'
},
{
name: 'ng-minlength',
value: 3
}
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment