【问题标题】:Nested unique ng-model per ng-repeat每个 ng-repeat 嵌套的唯一 ng-model
【发布时间】:2015-10-12 14:07:06
【问题描述】:

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

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

控制器:

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 名球员。这将创建一个包含 5 个重复表的页面,每行包含 4 名球员和一个球队名称。

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

数据库有以下列(如有需要可更改):

  • team_id(唯一,插入时创建)
  • league_id(每个团队唯一,这是从工厂继承的)
  • 团队名称
  • player_1
  • player_2
  • player_3(接受空值)
  • player_4(接受空值)
  • player_5(接受空值)
  • player_6(接受空值)
  • 站着
  • 付费
  • 创建于
  • 更新时间

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

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

【问题讨论】:

  • 我认为您正在向后看。如果您在控制器中设计您想要的模型,ng-repeat 将就位。您采用的方法更像是 JQuery 的思维方式,尝试基于 DOM 构建模型。这是行不通的,因为关于无限摘要的多条消息可能会证明这一点。我现在正在查看代码,看看我是否可以建议有角度的方式来做到这一点。
  • @Vanojx1 刚刚发布了一个类似于我已经开始玩弄的结构。

标签: javascript angularjs


【解决方案1】:

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

$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

【讨论】:

  • http.post 请求是什么样的?
  • 我会添加请求
  • 你的后端是基于 php 的吗??
  • 谢谢。我今天会试试这个答案。
  • 不,先生。 NodeJS/Postgres。
【解决方案2】:

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

myApp.controller('teamController', function($scope) {
    $scope.team={};
    $scope.league = { teams: 5,
                     format: 4}

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

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

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container" ng-app="myApp" ng-controller="teamController">
    <h1>Create Teams</h1>

    <table  ng-repeat="p 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_'+($index+1)].team_name"/></td>
        </tr>
        <tr ng-repeat="p in toArray(league.format) track by $index" ng-init="innerIndex=$index">
            <td>Player  {{$index + 1}}</td>
            <td><input type="text" ng-model="team['team_'+($parent.$index+1)].player['player_'+(innerIndex + 1)]"/></td>

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

    <button ng-click="log(team)">Test</button>
  <div>
  JSON: <br>{{team}}
  </div>

</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多