简体   繁体   English

如何从 ng-repeat 项目填充表单

[英]How can I populate a form from an ng-repeat item

I want to be able to click on an ng-repeat ed row in a table and populate a form from the rows data.我希望能够单击表格中的 ng-repeat ed 行并从行数据填充表单。 What do I need to have in my editplayer() function?我的 editplayer() 函数需要什么?

Heres the table and the form这是表格和表格

 <table class="table table-condensed table-striped table-responsive"> <thead> <th></th> <th>Name</th> <th></th> <th></th> <th></th> <th></th> </thead> <tbody> <tr ng-repeat="player in players" ng-model="players"> <td><input type="checkbox" name="" id=""></td> <td ng-click="editPlayer($index)">{{ player.name }}</td> <td><i class="fa fa-paper-plane"></i></td> <td><i class="fa fa-comment"></i></td> <td><i class="fa fa-phone"></i></td> <td><i class="fa fa-edit"></i></td> </tr> </tbody> </table>

 <form id="playerForm" name="playerForm" ng-model="thisplayer" novalidate> <input type="text" id="id" name="id" placeholder="id" class="form-control" ng-model="player.id" ></input> <div class="form-group"> <input type="text" id="player" name="name" required placeholder="Name" class="form-control" ng-model="player.name" ng-required="true" >{{ player.name }}</input> <span ng-show="playerForm.name.$touched && playerForm.newplayer.name.$invalid">The name is required.</span> </div> <div class="form-group"> <input type="email" id="email" required Placeholder="Email" class="form-control" ng-model="player.email">{{ player.email }} </input> </div> <div class="form-group"> <input type="text" id="tel" required Placeholder="Tel" class="form-control" ng-model="player.tel">{{ player.tel }}</input> </div> <div class="form-group"> <input type="text" id="initials" required Placeholder="Initials" class="form-control" ng-model="player.initials">{{ player.initials }}</input> </div> <div class="form-group"> <button id="btnPlayerAdd" name="btnPlayerAdd" class="btn btn-success" type="submit" ng-click="addPlayer($event)" ng-show="!isUpdate">Add player</button> <button id="btnPlayerSave" name="btnPlayerSave" class="btn btn-success" type="submit" ng-click="updatePlayer($event)" ng-show="isUpdate">Update player</button> </div> </form>

You can follow this:你可以按照这个:

  <tr ng-repeat="player in players">
        <td>{{player.Name}}</td>
        <td><input type="button" value="Edit" class="btn btn-info btn-sm" ng-click="editplayer(player)" /></td>

    </tr>

And in editplayer() :在 editplayer() 中:

$scope.editplayer= function (player) {
        $scope.player.id = player.Id;
        $scope.player.name = player.Name;
      }

Use editPlayer(player) .使用editPlayer(player)

Assuming editPlayer populates the form, just pass it the player object.假设editPlayer填充表单,只需将player对象传递给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM