【问题标题】:Angular-meteor use of ng-model inside ng-repeatng-repeat 中 ng-model 的 Angular-meteor 使用
【发布时间】:2016-06-27 02:18:10
【问题描述】:

我正在尝试使用 ng-repeat 中的重复变量作为 ng-model 的一部分,该变量被传递给控制器​​以插入 MongoDB。

在下面的html中,我使用了

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

我真的想要每个列字段的输入

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

但是,这会产生错误。也没有

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

将 column.field 作为字符串。

任何想法如何让它发挥作用?我尝试使用 getter/setter 函数,但不确定如何正确执行此操作,或者是否有必要。

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() {
  this.person.owner = Meteor.user()._id;
  Persons.insert(this.person);

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

  this.reset();
}

【问题讨论】:

    标签: angularjs angular-meteor


    【解决方案1】:

    ng-model 应该是一个对象。

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

    没有 ng-repeat:

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

    使用 ng-repeat:

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

    检查这个答案:https://stackoverflow.com/a/14411131/3967587

    还有这个小提琴:https://jsfiddle.net/win_web/hrok3944/

    更新

    您仍在寻找问题的答案,请创建一个小提琴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 2014-10-30
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多