1

I am trying to use the repeated variable from an ng-repeat in as part of the ng-model, which is passed to the controller to be inserted into MongoDB.

In the html below, I have used

<input... ng-model="tablesList.person.lastName />

where I really want an input for each column field

<input... ng-model="tablesList.person.{{column.field}} />

however, this produces an error. Neither does

<input... ng-model="tablesList.person.column.field />

which takes column.field as a string.

Any ideas how to get this to work? I have tried to use getter/setter function, but am not sure how to do this properly, or if this is necessary.

HMTL:

<form name="newTableRowForm" novalidate ng-cloak>
   <td ng-repeat="column in tablesList.ammiTables">
      <md-input-container flex class="md-block">
         <input type="text" ng-model="tablesList.person.lastName" />
      </md-input-container>
   </td>
   <td> 
      <md-button ng-click="tablesList.submit()" class="md-primary" ng-disabled="newTableRowForm.$invalid" aria-label="send">
         Submit <md-icon md-svg-icon="content:ic_send_24px"></md-icon>
      </md-button>
   </td>
 </tr>
</form>

Submit part of controller:

submit() {
  this.person.owner = Meteor.user()._id;
  Persons.insert(this.person);

  if(this.done) {
    this.done();
  }

  this.reset();
}
npasco
  • 59
  • 6

1 Answers1

0

ng-model should be an object.

$scope.lines = [
    {
        text: 'res1'
    },
    {
        text: 'res2'
    }
  ];

without ng-repeat:

<input type="text" ng-model="lines[0].text">

with ng-repeat:

<div ng-repeat="line in lines">
  <input type="text" ng-model="line.text[$index]">
</div>

check this answer: https://stackoverflow.com/a/14411131/3967587

and this fiddle: https://jsfiddle.net/win_web/hrok3944/

update

you are still looking for answers to the question, please create a fiddle.

Community
  • 1
  • 1
Cem Arguvanlı
  • 643
  • 10
  • 15