0

如何ngRepeat从控制器访问循环内的给定记录,最终用于表单验证?

我有以下表格,可以让用户动态添加/删除记录:

<table class="table table-condensed span12" id="tableParticipants">
  <!-- snip -->
  <tbody>
    <tr ng-repeat="person in people">
      <td><input type="text" pattern="[0-9]*" data-provide="typeahead" placeholder="8675309" ng-model="person.empid"></td>
      <td><input type="text" data-provide="typeahead"  placeholder="Firstname Lastname" ng-model="person.name"></td>
      <td><input type="text" placeholder="Title" ng-model="person.title"></td>
      <td style="text-align: center"><i class="icon-remove" ng-click="removeParticipant()"></i></td>
    </tr>
  </tbody>
</table>

在提交表单时,我需要确保员工 ID 有效,否则我会收到数据库错误。所以,当我通过循环工作时:

$scope.people.forEach(function(element, index, array)
{
  if ($element['empid'] == BAD)
  {
    // set corredponding ngRepeat row to error
  }
}

如何访问特定记录的给定行?

4

1 回答 1

2

您应该能够执行以下操作。将 css 类名称添加到数组项和/或错误消息。其余的应该由 Angular 处理,它将更新。您可以选择显示/隐藏消息、添加类等。

<tr ng-repeat="person in people" ng-class="person.error">
 <td><input type="text" pattern="[0-9]*" data-provide="typeahead" placeholder="8675309" ng-model="person.empid"></td>
  <td><input type="text" data-provide="typeahead"  placeholder="Firstname Lastname" ng-model="person.name"></td>
  <td><input type="text" placeholder="Title" ng-model="person.title"></td>
  <td style="text-align: center"><i class="icon-remove" ng-click="removeParticipant()"></i> <span>{{person.errorMessage}}</td>
</tr>

if ($element['empid'] == BAD)
{
  $element['errorMessage'] = 'Invalid Id';  // could bind or show a span/depends.
  $element['error'] = 'error';  // set to whatever class you want to use.
}
于 2013-04-30T22:14:26.357 回答