javascript - how to display checked values in checkbox using angular array? -
i've accomplished selecting htc in home page , coming page. want display whether selected store value , severdata match (in case need show true). during on submit event, want pass selected values. have tried code below, not working me.
$scope.selctedstores =window.localstorage.getitem("selectedservices"); console.log($scope.selctedstores); //console getting htc var serverdata = ["nokia", "htc", "samsung"]; $scope.items = []; (var = 0; < serverdata.length; i++) { var modal = { name: serverdata[i], selected: false }; $scope.items.push(modal); } $scope.check = function() { var checkeditems = []; (var = 0; < $scope.items.length; i++) { if ($scope.items[i].selected) { checkeditems.push($scope.items[i].name); } } console.log(checkeditems); }
html
<div ng-controller="test1controller"> <div ng-repeat="item in items"> <input type="checkbox" ng-model="item.selected" /> {{item.name}} </div> <input type="button" name="submit" value="submit" ng-click="check()" /> </div>
before pushing items array check whether item present in selected store. if so, assign selected true. hope helps. let me know if have problem
check updated fiddle
function todoctrl($scope) { var serverdata = ["nokia", "htc", "samsung"]; var selectedstore = ["htc"] $scope.items = []; (var = 0; < serverdata.length; i++) { var modal = { name: serverdata[i], selected: false }; if (selectedstore.indexof(serverdata[i]) >= 0) { modal.selected = true; } $scope.items.push(modal); } $scope.check = function() { var checkeditems = []; (var = 0; < $scope.items.length; i++) { if ($scope.items[i].selected) { checkeditems.push($scope.items[i].name); } } console.log(checkeditems); } }
Comments
Post a Comment