3

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?

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.