Wednesday, April 29, 2015

AngularJS Add Attribute to All Form Element


First of all we need to get all form element, which is element with class attribute name "form-control".

Then I use $set to add attribute in all element. Don't confuse with scope.form.tooltip, it is actually variable I declare from config.

  1. angular.module('schemaForm').directive('formControl', function () {
  2. return {
  3. restrict: 'C',
  4. priority: 1001,
  5. replace: false,
  6. link: function(scope, element, attrs) {
  7. if(scope.form.tooltip !== undefined){
  8. attrs.$set('tooltips', '');
  9. attrs.$set('title',scope.form.tooltip);
  10. }
  11. }
  12. };
  13. });

Initially I use $compile to add attribute, I found this is more light weight to perform the work.

Happy coding!