【发布时间】:2015-07-08 07:45:17
【问题描述】:
如何通过单击按钮在重复列表中选中复选框项目的 ID 并将项目添加到变量/数组以供以后使用?
html:
<input id="btnCheck" type="button" value="Next" ng-click="addSelected()" />
<div ng-controller="movieController">
<ul>
<li ng-repeat="movie in Movies">
<input id="chkBox-{{ movie.MovieID }}"
type="checkbox"
ng-checked="selection.indexOf(movie.MovieID) > -1"
ng-click="toggleSelection(movie.MovieID)"
/>
</li>
</ul>
</div>
脚本:
$scope.AddSelected = function () {
var selected = $scope.selection
console.log(selected);
}
$scope.selection = [];
$scope.toggleSelection = function toggleSelection(movie) {
var idx = $scope.selection.indexOf(movie);
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
else {
$scope.selection.push(movie);
}
};
【问题讨论】:
-
实际上该帖子中的答案对我有所帮助,但并非完全如此。做出选择后,如何通过单击按钮访问 $scope.selection 值,例如 console.log 并查看对象或将其保存在会话变量中。也许我对这一切的态度都是错误的。跟我来吧,伙计们,我还是很新鲜到有棱角的。
标签: javascript angularjs checkbox angularjs-ng-repeat