Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular element wrapper directive

Here is a code snippet we usually see in twitter boostrap forms:

 <div class="control-group">
    <label class="control-label" for="email">Enter Email</label>
    <div class="controls">
       <input type="email" name="email" ng-model="member.email" required >
    </div>
 </div>

Having lots of fields in form code gets quite noisy so I would like to use something like this in my Angular powered html:

<formy label-for="email" label-text="Enter Email">
      <input type="email" name="email" ng-model="member.email" required >
</formy>

Can this be done by directive in Angular?

like image 442
dzolnjan Avatar asked Aug 24 '26 07:08

dzolnjan


1 Answers

Yes it can be done

app.directive('formy', function() {
  return {
        restrict: 'E',
        transclude: true,
        scope: {
            labelText: "@",
      labelFor: '@'
        },
        template : '<div class="control-group">' +
        '<label for="{{labelFor}}" class="control-label">{{labelText}}</label>' +
        '<div class="controls" ng-transclude></div>' +
        '</div>', 
        replace: true
    };
})

Demo: Plunker

like image 67
Arun P Johny Avatar answered Aug 26 '26 23:08

Arun P Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!