javascript - 每个 ng-repeat 嵌套唯一的 ng-model

标签 javascript angularjs

当前正在创建一个包含嵌套 ng-repeats 和 ng-models 的页面。 ng-models 基于 $index。

http://codepen.io/natdm/pen/vOzaPj?editors=101 (因为 jsfiddle 有一个过时的 Angular)

Controller :

var myApp = angular.module('myApp', []);

myApp.controller('teamController', function($scope) {

    $scope.league = { teams: 5,
                     format: 4}

    $scope.toArray = function(team) {
        return new Array(team);
    };

    $scope.log = function(data) {
        console.log(data);
    };

});

HTML:

<div class="container" ng-app="myApp" ng-controller="teamController">
    <h1>Create Teams</h1>

    <table ng-repeat="t in toArray(league.teams) track by $index">
        <thead class="col-md-6">
        <tr>
            <th>Team {{$index + 1}}</th>
        </tr>
        </thead>
        <tbody class="col-md-6">
        <tr>
            <td>Team Name</td>
            <td><input type="text" ng-model="team.team_name"/></td>
        </tr>
        <tr ng-repeat="p in toArray(league.format) track by $index">
            <td>Player {{$index + 1}}</td>
            <td><input type="text" ng-model="team.player_+[$index + 1]"/></td>

        </tr>
        </tbody>
    </table>

    <button ng-click="log(team)">Test</button>

</div>

我正在从工厂中提取团队数量和每个团队的玩家数量。它们只相当于两个数字。可能有 5 支球队,每支球队有 4 名球员。这将创建一个包含五个重复表的页面,每行包含 4 个玩家和一个团队名称。

我正在想办法将这些发送到数据库。由于此页面是根据球队和球员的数量呈现的,因此制作 ng-models 的唯一方法是使用 $index。

数据库包含以下列(如有需要,可能会发生变化):

  • team_id(唯一,插入时创建)
  • league_id(每个团队唯一,这是从工厂继承过来的)
  • 团队名称
  • 玩家_1
  • 玩家_2
  • player_3(接受 null)
  • player_4(接受 null)
  • player_5(接受 null)
  • player_6(接受 null)
  • 站着
  • 付费
  • 创建时间
  • 更新时间

如何让包含重复团队字段的页面通过一个 $http.put 请求同时上传多个团队?

此外,也许是一个较小的问题,按照我设置的方式,它会强制每个文本的输入成为 ng-model 名称。那必须改变。

最佳答案

我认为你需要在一个新的结构中解析你的结构

$scope.parseLeague = [];

for(var i=0;i<$scope.league.teams ;i++){
  $scope.parseLeague.push({team: "team_"+i, players: []});
  for(var j=0;j<$scope.league.format;j++){
    $scope.parseLeague[i].players.push("player_"+j);
  }
}

然后在 html 中,您需要使用新结构更改重复

<table ng-repeat="team in parseLeague track by $index">

<tr>
    <td>Team Name</td>
    <td><input type="text" ng-model="team.name"/></td>
</tr>

<tr ng-repeat="player in team.players track by $index">
    <td>Player {{$index + 1}}</td>
    <td><input type="text" ng-model="player"/></td>
</tr>

http请求

var request = $http({
    method: "post",
    url: "yoururl",
    data: { teamsdata: angular.toJson($scope.parseLeague)}
});
request.success(
    function( risp ) {
        //handle your risp
    }
);

工作笔http://codepen.io/anon/pen/xGaaRP?editors=101

关于javascript - 每个 ng-repeat 嵌套唯一的 ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31567533/

相关文章:

javascript - 我如何处理未兑现的 promise 的 Angular-UI-Router 解析?

javascript - AngularJS Web应用程序查看页面源仅显示 "index.html"内容

angularjs - 选择 ng-repeat 复选框 Selenium

javascript - innerHTML 中的字符数

javascript - 无法将 javascript 中的 json api 正确打印到 html

php - 类似于 niftyplayer 的可编写脚本的 mp3 播放器

javascript - 操作方法 : Creating Angular/Bootstrap btn-checkboxes in a repeater

angularjs - 与 leaflet.heat 一起使用时,传单 CircleMarker 事件不起作用

javascript - JavaScript 上的火星到漫游者。我需要能够向流动站发出一系列命令并按顺序执行它们。

javascript - 如何使用 jQUery 中的数据属性更新表中的数据