2

After reading this article, I understand how to dynamically add a form field using ng-repeat.

I am wondering how can multiple form elements be dynamically created with incrementing ng-model values.

For example, the following would be created from a button click.

<input ng-model="vm.foo.bar1.first">
<input ng-model="vm.foo.bar1.second">
<input ng-model="vm.foo.bar1.third">
<input ng-model="vm.foo.bar1.fourth">

<input ng-model="vm.foo.bar2.first">
<input ng-model="vm.foo.bar2.second">
<input ng-model="vm.foo.bar2.third">
<input ng-model="vm.foo.bar2.fourth">

<input ng-model="vm.foo.bar3.first">
<input ng-model="vm.foo.bar3.second">
<input ng-model="vm.foo.bar3.third">
<input ng-model="vm.foo.bar3.fourth">

How can this be done?

3 Answers 3

1

I would suggest to restructure your ViewModel to make vm.foo.bar an array. Then this would be trivial:

<div ng-repeat="item in barItems">
  <input ng-model="vm.foo.bar[$index].first">
  <input ng-model="vm.foo.bar[$index].second">
  <input ng-model="vm.foo.bar[$index].third">
  <input ng-model="vm.foo.bar[$index].fourth">
</div>

Or, if you insist, then also

<div ng-repeat="item in barItems" ng-init="outerIdx = $index">
   <input ng-repeat='p in ["first", "second", "third", "fourth"]' 
          ng-model="vm.foo.bar[outerIdx][p]">
</div>

(I'm assuming here, that unlike with first, second, etc..., the number of bars is not known - hence an array is a better option)

EDIT:

If you really want, you could also make vm.foo an object that holds properties bar1, bar2, etc...:

<div ng-repeat="item in [1, 2, 3, 4]">
  <input ng-model="vm.foo['bar' + item].first">
  <input ng-model="vm.foo['bar' + item].second">
  <input ng-model="vm.foo['bar' + item].third">
  <input ng-model="vm.foo['bar' + item].fourth">
</div>

but don't forget to first create vm.foo object in the controller:

this.foo = {};
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your thorough answer. In my case, vm was set to this in the controller to replace $scope.
1

When I have to do this I use the $index to control the names of things. Although I've never tried this exact code, this should work.

<input ng-model='vm.foo.bar3[$index]'></input>

$index comes along whenever you do ng-repeat and is just the index of the list item. So that should end up making ng-models that are vm.foo.bar3.0 to whatever.

Comments

0

To my point of view, you should create arrays of models in your controller.

$scope.vm.foo = [{
    bar1: [{
        first: '',
        second: '',
        ...
       },
    bar2: ...
  ],
}]

And then in your view iterate on your tab :

<div ng-repeat="elem in foo">
    <div ng-repeat="input in elem">
        <input ng-model="input">
    </div>
</div> 

Hope it will help you !

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.