Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm using AngularJS and trying to create a form where I can dynamically add new inputs, similar to this fiddle: http://jsfiddle.net/V4BqE/ (Main HTML below, working code in fiddle).

<div ng-app="myApp" ng-controller="MyCtrl">
  <div add-input>
    <button>add input</button>
  </div>
</div>

I would like to be able to use a HTML template for my form since the input I'm adding is ~300 lines long. My issue is I cannot figure out how to index the scope variable containing the data when used in a template. I've tried to make my own modified version of the above code on plnkr http://plnkr.co/edit/4zeaFoDeX0sGTuBMCQP2?p=info . However, when I click the button no form elements appear.

Online (plnkr) I get a 404 not found for my template.html, but I think that is just a plnkr limitation. On my machine with a Python HttpServer I get an Error: [$parse:syntax] for the $templateRequest and a TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. when using the $http.get method.

Any advice for getting the indexed scope variable array to work with an external html file?

Thanks, JR

Edit: Update plnkr link

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You can do it without directive & external template

what you are trying to do does not require a directive (it actually much simple with the basic angularjs tools)

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

html

<body>

  <div class="container" ng-app="myApp" ng-controller="MyCtrl">

    <button class="btn btn-primary" type="button" ng-click="addPhoneInput()">add input</button>

    <form>
      <div ng-repeat="item in telephoneNumbers">
        <hr>
        <input type="text" ng-model="item.phone">
      </div>
    </form>


    <hr>

    <div class="well">
      <h4>Phone Numbers,</h4>
      <p ng-repeat="item in telephoneNumbers">{{item.phone}}</p>
    </div>
  </div>
</body>

js

var app = angular.module('myApp', []);

app.controller('MyCtrl', ['$scope', function($scope) {
  // Define $scope.telephone as an array
  $scope.telephoneNumbers = [];

  $scope.addPhoneInput = function() {
    $scope.telephoneNumbers.push({});
  };

  // This is just so you can see the array values changing and working! Check your console as you're typing in the inputs :)
  $scope.$watch('telephoneNumbers', function(value) {
    console.log(value);
  }, true);
}]);

If you insist using a directive,

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

phone-number.template.html

<div>
  <hr>
  <input type="text" ng-model="ngModel" >
</div>

html

<body>

  <div class="container" ng-app="myApp" ng-controller="MyCtrl">

    <button class="btn btn-primary" type="button" ng-click="addPhoneInput()">add input</button>

    <form>
      <phone-number ng-repeat="item in telephoneNumbers" ng-model="item.phone"></phone-number>
    </form>


    <hr>

    <div class="well">
      <h4>Phone Numbers,</h4>
      <p ng-repeat="item in telephoneNumbers">{{item.phone}}</p>
    </div>
  </div>
</body>

js

var app = angular.module('myApp', []);

app.controller('MyCtrl', ['$scope', function($scope) {
  // Define $scope.telephone as an array
  $scope.telephoneNumbers = [];

  $scope.addPhoneInput = function() {
    $scope.telephoneNumbers.push({});
  };

  // This is just so you can see the array values changing and working! Check your console as you're typing in the inputs :)
  $scope.$watch('telephoneNumbers', function(value) {
    console.log(value);
  }, true);
}]);

app.directive('phoneNumber', function(){
  return {
    replace:true,
     scope: {
      ngModel: '=',
    },
    templateUrl: "phone-number.template.html"
  }
});
share|improve this answer
    
Thanks for the answers! I will try these when I get back to my programming computer this evening. – John Robinson Jun 5 at 15:14
    
This is fantastic, does what I want. I slightly modified your directive plnkr to include having multiple fields in the template. (plnkr.co/edit/9w0SXVJFABj9NgxVCQJ1?p=preview) I'm using a directive because I want the reusability and the separation of the HTML template from the main index.html, any reasons to avoid using one? – John Robinson Jun 7 at 17:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.