首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >从ngRepeat访问元素

从ngRepeat访问元素
EN

Stack Overflow用户
提问于 2013-05-01 06:07:04
回答 1查看 572关注 0票数 0

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

我有以下表单,其中可以包含用户动态添加/删除的记录:

代码语言:javascript
代码运行次数:0
运行
复制
<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是有效的,否则将得到一个数据库错误。所以,当我完成这个循环时:

代码语言:javascript
代码运行次数:0
运行
复制
$scope.people.forEach(function(element, index, array)
{
  if ($element['empid'] == BAD)
  {
    // set corredponding ngRepeat row to error
  }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-01 06:14:26

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

代码语言:javascript
代码运行次数:0
运行
复制
<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.
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16309760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档