带有ng-repeat的空表行,AngularJS

时间:2014-07-18 09:52:47

标签: javascript html angularjs html-table

我有可以提交的输入字段,并且您输入的数据将插入到数据库中。然后将该数据循环到具有ng-repeat的表格中

    <table class="table table-striped table-condensed">
        <thead>
            <tr>
                <th>Sträcka</th>
                <th>Tid</th>
                <th>Jämför</th>
             </tr>
        </thead>
            <tr ng-repeat="info in test"><td>{{info.stracka}}</td><td>{{info.tid}}</td><td><input type="checkbox" id="{{info.id}}" class="checkboxfisk" ng-click="testa(info.id)"></tr>
        </table>

问题在于,当数据库表为空,并且您是第一次提交数据时,在您点击提交后会打印出3个空的TR行。我使用firebug来调试它,当我将鼠标悬停在空的TR行上时,HTML看起来像这样:

<table class="table table-striped table-condensed">
<thead>
<tr>
<th>Sträcka</th>
<th>Tid</th>
<th>Jämför</th>
</tr>
</thead>
<tbody>
<tr class="ng-scope" ng-repeat="info in test">
<td class="ng-binding"></td>
<td class="ng-binding"></td>
<td>
<input id="" class="checkboxfisk" type="checkbox" ng-click="testa(info.id)">
</td>
</tr>
<tr class="ng-scope" ng-repeat="info in test">
<td class="ng-binding"></td>
<td class="ng-binding"></td>
<td>
</tr>
<tr class="ng-scope" ng-repeat="info in test">
<td class="ng-binding"></td>
<td class="ng-binding"></td>
<td>
</tr>
</tbody>
</table>
<form class="ng-pristine ng-invalid ng-invalid-required" novalidate="" ng-submit="submitForm(userForm.$valid)" name="userForm">

如您所见,有一些类型为ng-binding的td。这是什么?任何人都可以帮助我吗?

这是我的控制器:

as.controller('Test', function($scope, $http, $rootScope, testFactory)
{   
    $http.get($rootScope.appUrl + '/nao/test/test')
        .success(function(data, status, headers, config) {
            $scope.test = data.data;
    });

    $scope.form = {};
    $scope.checkboxes = [];

    $scope.testa = function(id) {
        $scope.checkboxes.push(id);
    };

    $scope.submitForm = function(isValid) {
        if(isValid) 
        {   
            $http.post($rootScope.appUrl + '/nao/test', $scope.form)
            .success(function(data, status, headers, config) {
                console.log(data);
                console.log($scope.form);
            }).error(function(data, status) {

            });
        }
    };
});
编辑:问题发生在我的后端。我已经忘记添加一个在执行mysql查询后返回的参数。

1 个答案:

答案 0 :(得分:1)

我看到两种可能性。

首先,您应该在$ http调用之前初始化测试范围变量(以确保即使http请求失败也会初始化)。然后创建一个方法updateTest,它将在提交表单的成功时调用(更新测试变量)。

您的代码应该是这样的:

as.controller('Test', function($scope, $http, $rootScope, testFactory)
{   
    // METHOD to update test items
    updateTest = function() {
        $http.get($rootScope.appUrl + '/nao/test/test')
            .success(function(data, status, headers, config) {
                $scope.test = data.data;
        });
    };

    // init variables
    $scope.form = {};
    $scope.checkboxes = [];
    $scope.test = [];

    $scope.testa = function(id) {
        $scope.checkboxes.push(id);
    };

    $scope.submitForm = function(isValid) {
        if(isValid) 
        {   
            $http.post($rootScope.appUrl + '/nao/test', $scope.form)
            .success(function(data, status, headers, config) {
                console.log(data);
                console.log($scope.form);
                // update test item (to get back newly created one)
                updateTest();
            }).error(function(data, status) {

            });
        }
    };

    // first test items update
    updateTest();
});