【问题标题】:Multiple ng-repeat on a single view单个视图上的多个 ng-repeat
【发布时间】:2016-03-21 23:48:03
【问题描述】:

我正在尝试显示由 Web 服务中的 JSON 制成的表格,然后创建一个数组。数组长度为 0,当我单击一个按钮时,我将 1 个元素添加到数组并在表中添加一个新行。当我单击按钮时,不会将元素添加到数组中,也不知道为什么。在同一个控制器和视图上可以有 2 个不同的 ng-repeat 吗?

有控制器:

assets.controller('EditaTipusCtrl', function ($scope, $http, $routeParams) {
                $http.get('http://10.0.203.73/WS/ws.php/tipusactius/getDetails/' + $routeParams.id).success(function (data) {
                    $scope.atrb = data;
                });

                $scope.nousAtributs = [];

                $scope.addNewLine = function () {
                    var newRow = {
                        "nomAtribut": "",
                        "tipus": "",
                        "mida": '',
                        "prioritat": "",
                        "obligatori": "",
                        "observacions": "",
                        "nomTipusActiu": $routeParams.id // nom del tipus d'actiu
                    };
                    $scope.nousAtributs.push(newRow);
               //     return $scope.nousAtributs;
                }

            });

还有观点:

<div class="col-md-10">
    <div ng-controller="EditaTipusCtrl" id="test">
        <div class="row">
            <button class="btn btn-default" >
                <span class="glyphicon glyphicon-plus" aria-hidden="true" ng-click="addNewLine()"></span>
                Afegir atribut
            </button>
        </div>
        <br />
        <table class="table">
            <tr>   
                <th>#</th>
                <th><a href="" ng-click="sortField = 'ordre'; reverse = !reverse">Prioritat</a></th>
                <th><a href="" ng-click="sortField = 'nomAtribut'; reverse = !reverse">Atribut</a></th>
                <th><a href="" ng-click="sortField = 'midaAtribut'; reverse = !reverse">Mida</a></th>
                <th><a href="" ng-click="sortField = 'atributObligatori'; reverse = !reverse">Obligatori</a></th>
                <th><a href="" ng-click="sortField = 'observacions'; reverse = !reverse">Observacions</a></th>
            </tr>
            <tr ng-repeat="(key, value) in atrb">
                <td></td>
                <td>
                    <input type="number" ng-model="value.ordre" value="value.ordre" />
                </td>
                <td>
                    <input type="value.valor" ng-model="value.nomAtribut" value="value.nomAtribut" />
                </td>
                <td>
                    <input type="value.valor" ng-model="value.midaAtribut" value="value.midaAtribut" />
                </td>
                <td>
                    <input type="checkbox" ng-model="value.atributObligatori" value="value.atributObligatori" ng-true-value="'Si'" ng-false-value="'No'" />
                </td>
                <td>
                    <input type="value.valor" ng-model="value.observacions" value="value.observacions" />
                </td>
            </tr>
            <tr ng-repeat="a in nousAtributs">
                <td></td>
                <td>
                    <input type="number" ng-model="a.ordre" value="a.ordre" />
                </td>
                <td>
                    <input type="a.valor" ng-model="a.nomAtribut" value="a.nomAtribut" />
                </td>
                <td>
                    <input type="a.valor" ng-model="a.midaAtribut" value="a.midaAtribut" />
                </td>
                <td>
                    <input type="checkbox" ng-model="a.atributObligatori" value="a.atributObligatori" ng-true-value="'Si'" ng-false-value="'No'" />
                </td>
                <td>
                    <input type="a.valor" ng-model="a.observacions" value="a.observacions" />
                </td>
            </tr>
        </table> 
        <button class="btn btn-default" >
            <span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>
            Guardar
        </button> 
    </div>
</div>

已解决:

我把 ng-click 放在按钮标签上,而不是 span 标签上

问候

【问题讨论】:

  • 您是否在控制台中遇到错误?您在newRow 上设置的属性似乎与您尝试在表中输出的属性不匹配。例如,您的第一列有ng-model="a.ordre",但您正在创建的newRow 对象上没有“ordre”属性。
  • 我把 ng-click 放在了 button 标签上,而不是 span 标签上。还是谢谢

标签: javascript arrays angularjs web-services angularjs-ng-repeat


【解决方案1】:

你的 ng-click 应该在按钮标签而不是跨度

【讨论】:

    【解决方案2】:

    我认为你想做的事情需要ng-repeat-start 和ng-repeat-end。所以你会的;

    <tr ng-repeat-start="(key, value) in atrb">
        <td></td>
        <td>
            <input type="number" ng-model="value.ordre" value="value.ordre" />
        </td>
        <td>
            <input type="value.valor" ng-model="value.nomAtribut" value="value.nomAtribut" />
        </td>
        <td>
            <input type="value.valor" ng-model="value.midaAtribut" value="value.midaAtribut" />
        </td>
        <td>
            <input type="checkbox" ng-model="value.atributObligatori" value="value.atributObligatori" ng-true-value="'Si'" ng-false-value="'No'" />
        </td>
        <td>
            <input type="value.valor" ng-model="value.observacions" value="value.observacions" />
        </td>
    </tr>
    <tr style="display:none;" ng-repeat-end></tr>
    <tr ng-repeat-start="a in nousAtributs">
        <td></td>
        <td>
            <input type="number" ng-model="a.ordre" value="a.ordre" />
        </td>
        <td>
            <input type="a.valor" ng-model="a.nomAtribut" value="a.nomAtribut" />
        </td>
        <td>
            <input type="a.valor" ng-model="a.midaAtribut" value="a.midaAtribut" />
        </td>
        <td>
            <input type="checkbox" ng-model="a.atributObligatori" value="a.atributObligatori" ng-true-value="'Si'" ng-false-value="'No'" />
        </td>
        <td>
            <input type="a.valor" ng-model="a.observacions" value="a.observacions" />
        </td>
    </tr>
    <tr style="display:none;" ng-repeat-end></tr>
    

    请参阅文档here

    【讨论】:

    • OP对ng-repeat的使用是正确的,不需要ng-repeat-start和ng-repeat-end。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2019-03-19
    相关资源
    最近更新 更多