如何使用angularjs动态添加行
How to add the rows dynamically with angularjs?
我使用代码通过单击添加行来添加行和 2 列。"我的需要是",首先填写输入字段中的值,之后,单击"添加项目"按钮,值必须显示在表结构中。我是开始者。无法使用 for 循环。任何人都可以解决这个问题。
尝试代码:https://jsfiddle.net/idris9791_/a7L832LL/
<html >
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Add Rows</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<body ng-controller="MainController" ng-app="MyApp">
<a href="#" class="button" ng-click="addRow()">Add Row</a>
<form>
First Name : <input name="myInput" ng-model="user.firstName" required>
First Name : <input name="myInput" ng-model="user.lastName" required>
</form>
<table>
<thead>
<tr>
<th>Some Header</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="rowContent in rows">
<td>{{rowContent.firstName}}</td>
<td>{{rowContent.lastName}}</td>
</tr>
</tbody>
</table>
<script>
angular.module('MyApp', [])
.controller('MainController', [ '$scope', function($scope) {
$scope.rows = [];
$scope.counter = 0;
$scope.addRow = function() {
$scope.row.push({
firstName: $scope.firstName,
lastName: $scope.lastName
});
$scope.counter++;
}
}]);
</script>
</body>
</html>
您必须实际将输入的输入值推送到 rows
数组中:
$scope.row.push({
firstName: $scope.firstName,
lastName: $scope.lastName
});
我会更新 html 以阅读:
<a href="#" class="button" ng-click="addRow()">Add Row</a>
First Name : <input ng-model="firstName" required>
Last Name : <input ng-model="lastName" required>
然后:
<tr ng-repeat="rowContent in rows">
<td>{{rowContent.firstName}}</td>
<td>{{rowContent.lastName}}</td>
</tr>
确保正确包含 angular,并且您的控制器也以正确的方式使用。
相关文章:
- 如何在Google柱状图中动态添加行/列
- 使用jQuery动态添加表并在其中动态添加行
- 将表行动态添加到表或tbody元素中
- 在change事件javascript上动态添加行
- 在表中动态添加行
- 在使用 javascript 动态添加行时,我正在增加索引值,但出现错误
- 动态添加行时计数器的增量
- 动态添加行时,启用/禁用Gridview中的验证程序
- 将行动态添加到具有动态接收列数的表中
- 将行动态添加到HTML中
- JavaScript 动态地向表中添加行
- 动态添加行后脚本不起作用
- 添加新行动态引导日期时间选择器不起作用
- 数据表可编辑插件在数据表中动态添加行后不起作用
- 使用 JS/jQuery 将行动态添加到表中时行消失
- 如何将行号添加到动态添加到 html 表中的行
- 如何使用 MVC 剃刀语法将行动态添加到 html 表中
- 如何使用angularjs动态添加行
- 使用jQuery自动完成功能动态添加行
- Material Design Lite表格-添加行动态打断格式