动态创建ng-model值

时间:2019-01-15 17:27:31

标签: angularjs angularjs-directive angularjs-scope

我正在构建一个债务合并计算器,该计算器具有两个允许用户添加和删除行的表。这些行包含4个不同值的输入:

代码插入器: plnkr.co/edit/AS6M8zi3VrqKKgfTGwVT?p=preview

HTML

 .directive('cuDebtConsolidation',
    function() {
        return {
            restrict: 'E',
            scope: false,
            template: 
            '<div  id="debtConsolidationPrint">' +
            '<form name="debtConsolidationForm" role="form" data-toggle="validator" novalidate>' +
               '<div class="row mb-3 mt-5 mx-auto calcRow">' +
                    '<div class="col m-3">' +
                        '<div ng-repeat="table in tables">' +
                            '<h3 class="mt-4">{{table.name}}</h3>' +
                            '<table>' +
                                '<thead>' +
                                    '<tr>' +
                                        '<th>Loan Name</th>' +
                                        '<th>Remaining Balance</th>' +
                                        '<th>Monthly Payment</th>' +
                                        '<th>Loan Term</th>' +
                                    '</tr>' +
                                '</thead>' +
                                '<tbody>' +
                                    '<tr ng-repeat="(rowIndex, row) in table.rows" name="{{row.name}}">' +
                                        '<td><input class="form-control form-control-md text-transform-none" title="Please Enter loan name" maxlength="7" required type="text"></td>' +
                                        '<td><input class="form-control form-control-md text-transform-none" title="Please Enter remaining balance" maxlength="7" required type="number"></td>' +
                                        '<td><input class="form-control form-control-md text-transform-none" title="Please Enter Monthly Payment" maxlength="7" required type="number"></td>' +
                                        '<td><input class="form-control form-control-md text-transform-none" title="Please Enter loan term" maxlength="7" required type="number" ></td>' +
                                        '<td><input type="button" class="btn btn-round btn-sm btn-brand" value="Remove" ng-click="removeRow(rowIndex, table)"/></td>' +
                                    '</tr>' +
                                '</tbody>' +
                            '</table>' +
                            '<button class="btn btn-round btn-sm btn-brand mt-2" ng-click="addRow(table)">Add Row</button>' +
                        '</div>' +
                        '<div class="d-flex">' +
                            '<button class="btn btn-round btn-lg btn-brand d-block mt-2 w-100" ng-click="debtConsolidation();">Calculate!</button>' +
                        '</div>' +
                    '</div>' +
                '</div>' +
            '</form>' +
        '</div>'
        };
    });

控制器:

 $scope.tables=[{name: "Installment Table"}, {name: "Credit Card Table"}];

        $scope.tables[0].rows = [{name: "row1"}];
        $scope.tables[1].rows = [{name: "row1"}];

        $scope.counter = 2;

        $scope.addRow = function(table) {
            table.rows.push({ name: "row" + $scope.counter});

            $scope.counter++;
        };

        $scope.removeRow = function(rowIndex, table) {
            table.rows.splice(rowIndex, 1);
        };

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

我使用ng-repeat遍历两个表,然后使用ng-repeat遍历行。这一切都存在于表单中,我尝试ng-click="debtConsolidation();"进行计算,但似乎无法弄清楚如何绑定动态输入,然后才能使用每个动态行中的数据。

1 个答案:

答案 0 :(得分:2)

You can give each input an ng-model attribute. This will put a value property on your $scope.tables.rows object.

Here's an example:

'<tbody>' +
   '<tr ng-repeat="(rowIndex, row) in table.rows" name="{{row.name}}">' +
      '<td><input ng-model="row.loanName" class="form-control form-control-md text-transform-none" title="Please Enter loan name" maxlength="7" required type="text"></td>' +
      '<td><input ng-model="row.remainingBalance" class="form-control form-control-md text-transform-none" title="Please Enter remaining balance" maxlength="7" required type="number"></td>' +
      '<td><input ng-model="row.monthlyPayment" class="form-control form-control-md text-transform-none" title="Please Enter Monthly Payment" maxlength="7" required type="number"></td>' +
      '<td><input ng-model="row.loanTerm" class="form-control form-control-md text-transform-none" title="Please Enter loan term" maxlength="7" required type="number" ></td>' +
      '<td><input type="button" class="btn btn-round btn-sm btn-brand" value="Remove" ng-click="removeRow(rowIndex, table)"/></td>' +
   '</tr>' +
'</tbody>'

And if you console.log the object in the $scope.debtConsolidation function, you can see the object.