Skip to content

Instantly share code, notes, and snippets.

@presstube
Created December 11, 2012 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save presstube/4255541 to your computer and use it in GitHub Desktop.
Save presstube/4255541 to your computer and use it in GitHub Desktop.
angularjs directive optional attributes
angular.module('formComponents', [])
.directive('formInput', function() {
return {
restrict: 'E',
compile: function(element, attrs)
{
var type = attrs.type || 'text';
var required = attrs.hasOwnProperty('required') ? "required='required'" : "";
var htmlText = '<div class="control-group">' +
'<label class="control-label" for="' + attrs.formId + '">' + attrs.label + '</label>' +
'<div class="controls">' +
'<input type="' + type + '" class="input-xlarge" id="' + attrs.formId + '" name="' + attrs.formId + '" ' + required + '>' +
'</div>' +
'</div>';
element.replaceWith(htmlText);
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment