How can I automatically add labels to input fields?

I'm working on a bigger project with AngularJS. Therefore, I want to make the work for a single form as easy as possible. As we're also using bootstrap, the code for a single input field in a form is quite verbose, maybe like

<div class="control-group">
  <label class="control-label" for="inputEmail">Email</label>
  <div class="controls">
    <input type="text" id="inputEmail" placeholder="Email">
  </div>
</div>

If I could write a single tag like

<custom-input 
  label="Email" 
  name="inputEmail" 
  placeholder="Email" 
  type="text" 
  ... >
</custom-input>

instead, this would help to keep the code clean and the work simple.

To achive this, I'm working on a custom AngularJS directive. My directive currently uses a template similar to the bootstrap example from above, automatically assigning the label to the input-tag. Also, the directive's compiler function moves all attributes from the custom-input tag to the real input-tag in order to make it easy to customize the custom-input tag.

app.directive('customInput', function() {
    return {
      require: 'ngModel',
      restrict: 'E',
      template: '<div>' + 
                    '<label for="{{ name }}">the label</label>' +
                    '<input id="{{ name }}" ng-model="ngModel" />' +
                '</div>',
      scope: {
              ngModel: '=',
              name: '@name',
          },
      replace: true,
      compile: function (tElement, tAttrs, transclude) {
            var tInput = tElement.find('input');

            // Move the attributed given to 'custom-input'
            // to the real input field
            angular.forEach(tAttrs, function(value, key) {
                if (key.charAt(0) == '$')
                    return;
                tInput.attr(key, value);
                tInput.parent().removeAttr(key);
            });

            return;
        },
    };
});

On Stack Overflow, there are many questions regarding the creation of custom input fields, but they are concerned with the data binding, custom formatting or binding to ng-repeat.

My approach however has a different issue: while the data binding works correctly, Angular's integrated form validation module get confused when the input field is 'required'. For some reason, validation doesn't recognize the new input field and instead keeps the form invalid because of some dead reference, which has an empty value. Please see the minimal example.

Where does the dead reference come from? How can I update the validation-module's references? Is there a better way to achieve my overall goal?


  • As a boolean attribute, there is a corresponding required property that is still true on your div even if the attribute is moved.
  • The required attribute isn't getting moved, it must be getting skipped because there is no value. I don't know how to even add it to an element using javascript without a value, but using the form required="required" fixes that
  • Using transclude=true will use a copy of your element after the compile phase when you moved attributes, I think this keeps the required property from being set
  • You have to assign a higher priority for some reason, maybe because of ng-model , which is not removed from your div because the name in tattrs is ngModel (although removing from the div doesn't remove the need for priority)
  • http://plnkr.co/edit/5bg8ewYSAr2ka9rH1pfE?p=preview

    All I did was change the required attribute to be required="required" and add these two lines to the directive declaration:

      transclude: true,
      priority: 10,
    

    I put ng-transclude on the template label by the way so the contents of your element will go in the label and you don't have to have an attribute for that.

    链接地址: http://www.djcxy.com/p/71860.html

    上一篇: 使用opengl es着色器将YUV转换为RGB

    下一篇: 我怎样才能自动添加标签输入字段?