数,用AngularJS打印选中的输入
Count & print the checked inputs with AngularJS
我使用ng-repeat打印输入复选框的列表。如何计算和打印已选中的复选框的数目?
JavaScriptvar app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.lists = [{'id':'1', 'name':'list 1'},
{'id':'2', 'name':'list 2'},
{'id':'3', 'name':'Macbook Pro'},
{'id':'4', 'name':'Dell Optiplex 755'},
{'id':'5', 'name':'Google Nexus S'}
];
});
HTML
<ul style="padding:10px;">
<input type="text" ng-model="fil.name" />
<li ng-repeat="list in lists | filter:fil">
<div style="margin-bottom:0;" class="checkbox">
<label>
<input type="checkbox" name="list_id[]" value="{{list.id}}" />
{{list.name}}
</label>
</div>
</li>
</ul>
http://plnkr.co/edit/4swnCWen370VhTuca0u2?p =预览
JS:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.lists = [
{'id':'1', 'name':'list 1', checked: true},
{'id':'2', 'name':'list 2'},
{'id':'3', 'name':'Macbook Pro'},
{'id':'4', 'name':'Dell Optiplex 755'},
{'id':'5', 'name':'Google Nexus S'}
];
$scope.$watch('lists', function(lists){
$scope.count = 0;
angular.forEach(lists, function(list){
if(list.checked){
$scope.count += 1;
}
})
}, true);
});
视图:
<body ng-controller="MainCtrl">
<ul style="padding:10px;">
<input type="text" ng-model="fil.name" />
<li ng-repeat="list in lists | filter:fil">
<div style="margin-bottom:0;" class="checkbox">
<label>
<input type="checkbox" name="list_id[]" ng-model="list.checked" value="{{list.id}}" />
{{list.name}}
</label>
</div>
</li>
</ul>
<strong>Count: {{count}}</strong>
</body>
Plnkr
相关文章:
- 按压“;输入“;将值打印到控制台日志
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- NodeJS readline有时会打印回第一个输入行(但并不总是,看起来是随机的)
- 如何在用户输入提交后在同一页面中打印出表单字段值
- 如何要求用户输入一个介于 1 和 50 之间的整数,然后小程序打印出一行这个星数
- 从一个复杂的JSON对象打印每个JSON输入路径
- Javascript:将多个表单输入打印到一个文本区域
- 我需要在提交时将HTML表单中输入的值打印在同一页面上
- 当用户在HTML中点击回车键时,我如何将输入打印到屏幕上
- 获取 td 值并通过 jquery 将其打印在输入文本中
- 如何在文本字段中输入数字时立即打印结果
- 打印所选颜色代码以输入值
- 读取输入和打印变量 - JavaScript 和 HTML 4.01
- 尝试使用输入值打印出 Javascript 数组
- 从打印预览中删除输入框
- 如何在jquery中组合打印和删除空输入
- 用于在同一页面中打印输入和文本区域的表单
- 如何从刚刚输入的输入字段中在每页上打印发票编号
- 当我在表单字段中输入数据并单击提交时,数据不会在浏览器和服务器中打印
- 我需要根据用户输入打印if或else语句,一旦所有输入都正确,我必须在按钮下打印用户输入