0

This is my controller:

$scope.inputs = [];
$scope.addInput = function () {
    $scope.inputs.push({
        id: $scope.inputs.length + 1,
    });

};
$scope.removeInput = function (index) {
    $scope.inputs.splice(index, 1);
};

I try this way but didn't work:

<div ng-repeat="input in inputs">
      <textarea   id="activity"
                  name="project_budget_administration[<% input.id %>][activity]"
                  placeholder="Activity"
                  ng-model="project_budget_administration.activity_<% input.id %>"
                  ng-maxlength="100"
                  required></textarea>
</div>

For name attribute of textarea works perfect but for ng-model - not. Also i tried with this syntax ng-model="project_budget_administration.activity[input.id]" and instead of input.id i tried $index, also not working.

1 Answers1

0

You can use javascript expressions to dynamically name the ng-model

<div ng-repeat="input in inputs">
  <textarea   id="activity"
              name="project_budget_administration[<% input.id %>][activity]"
              placeholder="Activity"
              ng-model="project_budget_administration['activity_' + input.id]"
              ng-maxlength="100"
              required></textarea>

Fiddle from a different answer - http://jsfiddle.net/DrQ77/ - How can I set a dynamic model name in AngularJS?

Community
  • 1
  • 1
KieranDotCo
  • 467
  • 2
  • 16