Reputation: 1263
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?
Upvotes: 3
Views: 2020
Reputation: 388316
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
Upvotes: 3