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.


angular.module('schemaForm').directive('formControl', function () {
  return {
     restrict: 'C',
     priority: 1001,
     replace: false,
      link: function(scope, element, attrs) {
        if(scope.form.tooltip !== undefined){
          attrs.$set('tooltips', '');
          attrs.$set('title',scope.form.tooltip);
        }
      }
  };
});

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

Happy coding!

No comments:

Post a Comment