2

I have an AngularJS directive that has an input element in its template. Now I can add this directive inside a <form> tag and bind the ng-model without any issues.

The issue that I am running into is that the input that is being dynamically added into the <form> is not showing up in the formContoller which makes proper validation impossible.

Is there a way to have a directive that is being included with a <form> that has its own scope be able to dynamically add inputs that will be included in the formController that the directive is included in?

UPDATE

So as I was trying to build out the simpler example, I did figure out the root cause of the issue and that is the fact I am using $compile to generate the template used. I have created a simplified version of the issue occurring on plunker:

http://plnkr.co/edit/XsyZKW?p=preview

AngularJS Code

angular.module('directive', []).directive('containerDir', function() {
  return {
    scope: {
      test: '@'
    },
    compile: function(element, attributes) {
      return function(scope, element, attributes) {
        scope.model = {
          dataObject: {}
        }
      };
    }
  };
});

angular.module('directive').directive('inputDir', function($compile) {
  return {
    template: '<span></span>',
    scope: {
      model: '=dataModel'
    },
    compile: function(element, attributes) {

      return {
        pre: function(scope, element, attributes) {
          var template = '<input type="text" name="username" ng-model="model.username" required />';
          element.html($compile(template)(scope));
        },
        post: function(scope, element, attributes) {
        }
      };
    }
  };
});

template

<!doctype html>
<html ng-app='directive'>
  <head>
  <script data-require="jquery" data-semver="2.0.1" src="http://code.jquery.com/jquery-2.0.1.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
    <script src="script.js"></script>

  </head>
  <body>
    <div container-dir>
      <form name="testForm" nag-resettable-form style="padding-left: 20px;">
        <div>
          <label for="first-name">First Name</label>
          <input id="first-name"
                 type="text"
                 name="firstName"
                 ng-model="model.dataObject.firstName"
                 required
          />
          <div>value: {{model.dataObject.firstName}}</div>
          <div>{{testForm.firstName.$error | json}}</div>
        </div>
        <div>
          <label for="username">Username</label>
          <span input-dir data-data-model="model.dataObject"></span>
          <div>value: {{model.dataObject.username}}</div>
          <div>{{testForm.username.$error | json}}</div>
        </div>
        <div>
          {{testForm | json}}
        </div>
        <div style="padding-top: 1rem;">
          <button class="primary" ng-click="model.submitForm()">Submit</button>
          <button ng-click="resetForm(formReveal, {}, model.resetForm)">Reset</button>
        </div>
      </form>
    </div>
  </body>
</html>

Here is the same example without using $compile to show it does work without it:

http://plnkr.co/edit/i8Lkt1?p=preview

Now I would still like to know if what I am trying to do it possible with adding input elements dynamically with directives that use $compile to generate the HTML for the directive.

While I might be able to rewrite the directive in question to not use $compile, I would still like to know if this is possible for future reference.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
ryanzec
  • 27,284
  • 38
  • 112
  • 169
  • can you post a example http://plnkr.co/ ? – Liviu T. Jun 24 '13 at 21:34
  • Is this at all helpful? http://stackoverflow.com/questions/12044277/how-to-validate-inputs-dynamically-created-using-ng-repeat-ng-show-angular/12044600#12044600 – Karen Zilles Jun 24 '13 at 22:08
  • I tried using ng-form with but it did not work (maybe I was not doing it correctly though). I also tried using formController.$addControl() and while that added a control to the formController, it was not linked properly to the template appending input (changes in the input would not trigger changes to that model). I will try to create a simplified directive to show this issue. – ryanzec Jun 24 '13 at 23:22

0 Answers0