如何使用angularjs向表中添加动态行

dev*_*evo 13 javascript angularjs angularjs-directive angularjs-scope

像jQuery一样,如何使用angularjs在按钮单击中使用表单元素向表中添加动态行,以及如何在常规jquery提交中区分这些表单元素,如数组名称.

<tr>
    <td style="text-align:center">1</td>
    <td>
         <input type="text" class="form-control"  required ng-model="newItem.assets">
    </td>
    <td>
        <select ng-model="newItem.type" class="form-control">
            <option value="Rent" ng-selected="'Rent'">Rent</option>
            <option value="Lease">Lease</option>
        </select>
    </td>
    <td>
        <input type="text" class="form-control"  required ng-model="newItem.amount" />
    </td>
    <td>
        <select ng-model="newItem.calculation_type" class="form-control">
            <option value="Daily" ng-selected="'Daily'">Daily</option>
            <option value="Monthly">Monthly</option>
            <option value="Yearly">Yearly</option>
        </select>
    </td>
    <td>
        <input type="text" class="form-control"  required ng-model="newItem.total_amount" />
    </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

Mat*_*ggi 18

重要的是要记住,使用Angular,您不是要向表中添加新行,而是修改模型的数据.模型更改时,视图将自动更新.您在示例中显示的仅仅是Angular将要执行的HTML模板部分.如前所述,您不会修改这些DOM元素,而应该操纵模型.所以这是我建议的步骤:

为您的表创建一个控制器

app.controller('CostItemsCtrl', [ '$scope', function($scope) {
  // the items you are managing - filled with one item just as an example of data involved
  $scope.items = [ { assets: 'My assets', type: 'Rent', amount: 500, 'calculation_type': 'Daily', total: 0}, ... ];
  // also drive options from here
  $scope.assetTypes = [ 'Rent', 'Mortgage' ];
  $scope.calcTypes = [ 'Daily', 'Monthly', 'Yearly' ];

  // expose a function to add new (blank) rows to the model/table
  $scope.addRow = function() { 
    // push a new object with some defaults
    $scope.items.push({ type: $scope.assetTypes[0], calculation_type: $scope.calcTypes[0] }); 
  }

  // save all the rows (alternatively make a function to save each row by itself)
  $scope.save = function() {
    // your $scope.items now contain the current working values for every field shown and can be parse, submitted over http, anything else you want to do with an array (like pass them to a service responsible for persistance)
    if ($scope.CostItemsForm.$valid) { console.log("it's valid"); }
  }
Run Code Online (Sandbox Code Playgroud)

使用HTML显示数据

<form name="CostItemsForm" ng-controller="CostItemsCtrl">
<table>
<tr><th>Assets</th><th>Type</th><th>Amount</th><th>Calculation</th><th>Total</th></tr>
<tr><td colspan="5"><button ng-click="addRow()">Add Row</button></td></tr>
<tr ng-repeat="item in items">
  <td><input required ng-model="item.assets"></td>
  <td><select ng-model="item.type" ng-options="type for type in assetTypes"></select></td>
  <td><input required ng-model="item.amount"></td>
  <td><select ng-model="item.calculation_type" ng-options="type for type in calcTypes"></td>
  <td><input required ng-model="item.total"></td>
</tr>
<tr><td colspan="5"><button ng-click="save()">Save Data</button></tr>
</table>
</form>
Run Code Online (Sandbox Code Playgroud)

(可选)添加CSS以在字段有效/无效时显示

input.ng-invalid {background-color:#FEE; 边框:纯红色1px}

"角度方式"

如您所见,您不会直接修改DOM.无需编写任何实际代码即可获得表单验证的所有优点.控制器通过保持模型并将各种功能暴露给其范围来纯粹作为控制器.您可以通过注入处理检索和更新数据的服务来进一步沿着角度路径,然后共享这些服务.也许您已经在代码中执行了此操作,但此代码应该适用于您的特定示例,而不需要任何其他依赖项.


lol*_*ski 3

您应该使用 来渲染该行ng-repeat,如下所示:

<form ng-submit="onSubmit(newItem)">
    <table>
    <tr>
        <td style="text-align:center">1</td>
        <td>
             <input type="text" class="form-control"  required ng-model="newItem.assets">
        </td>
        <td>
            <select ng-model="newItem.type" class="form-control">
                <option value="Rent" ng-selected="'Rent'">Rent</option>
                <option value="Lease">Lease</option>
            </select>
        </td>
        <td>
            <input type="text" class="form-control"  required ng-model="newItem.amount" />
        </td>
        <td>
            <select ng-model="newItem.calculation_type" class="form-control">
                <option value="Daily" ng-selected="'Daily'">Daily</option>
                <option value="Monthly">Monthly</option>
                <option value="Yearly">Yearly</option>
            </select>
        </td>
        <td>
            <input type="text" class="form-control"  required ng-model="newItem.total_amount" />
        </td>
    </tr>
    <tr ng-repeat="row in rows">
        <td>{{row.assets}}</td>
        <td>{{row.selected}}</td>
        <td>{{row.amount}}</td>
        <td>{{row.calculation_type}}</td>
    </tr>
    </table>
</form>
Run Code Online (Sandbox Code Playgroud)

您的控制器应如下所示:

angular.module('angularSimpleApp').controller('MyCtrl', function ($scope) {
    $scope.newItem = ''; // represents the models in the form
    $scope.rows = [];
    $scope.onSubmit = function(obj) {
        $scope.products.push(obj);
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 更新模型时,角度指令将负责将附加行添加到界面。这是一个工作示例http://plnkr.co/edit/VJSO588pXYBc72mC0sfg?p=preview (2认同)