javascript - Alert all checkboxes that are checked in ng-Repeat : AngularJS -
i have small program have 3 checkboxes. may check or of checkboxes. need alert names of people infront of id have checked checkboxes.
html:
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> </head> <body ng-app="myapp" ng-controller="myctrl"> <table> <tr ng-repeat="(key,x) in people"> <td><input type="checkbox" id="{{x.id}}" >{{x.name}}</td> </tr> </table> <button ng-click="alert()">click</button> <script> //module declaration var app = angular.module('myapp',[]); //controller declaration app.controller('myctrl',function($scope){ $scope.checkboxarray = []; $scope.people = [ {name:"peter",id:"201"}, {name:"lina",id:"202"}, {name:"roger",id:"203"} ]; //alert function $scope.alert = function(){ angular.foreach($scope.people, function(val,key) { if('#val.id:checked'){ $scope.checboxarray + = $scope.people[key].name; } alert($scope.checkboxarray); } } }); </script> </body> </html>
i not sure if putting condition inside if() condition. can help?
you need store checked status using ng-model
below find checked items using people
array
var app = angular.module('my-app', [], function() {}) app.controller('appcontroller', function($scope) { $scope.checkboxarray = []; $scope.people = [{ name: "peter", id: "201" }, { name: "lina", id: "202" }, { name: "roger", id: "203" }]; //alert function $scope.alert = function() { var selected = $scope.people.filter(function(item) { return item.checked }).map(function(item) { return item.name; }) alert(selected); } })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="my-app"> <div ng-controller="appcontroller"> <table> <tr ng-repeat="(key,x) in people"> <td> <input ng-model="x.checked" type="checkbox" id="{{x.id}}">{{x.name}}</td> </tr> </table> <button ng-click="alert()">click</button> <pre>{{people}}</pre> </div> </div>
Comments
Post a Comment