0

I want to print the values of a array based on the checkbox associated with it. Find the code below

Javascript:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName= [{name:"John",selected:"false"},{name:"Anil",selected:"false"},{name:"Kumar",selected:"false"}];
    $scope.lastName= "Doe";
    $scope.name1=[],
    $scope.addname=function(){
    angular.forEach($scope.firstName, function(name,selected){
  if(selected=="true") {
  alert(name);
    $scope.name1.push(name)
  }
});
 }
 });

html:

<div ng-app="myApp" ng-controller="myCtrl">
<table >

<tr ng-repeat="first in firstName">
<td><input type="Checkbox" ng-model="first.selected">{{first.name}}</td>

</tr>
<tr><td><input type="Button" ng-click="addname()" value="Submit" ng-model="lastName"></td></tr>

<tr ng-repeat="nam in name1">{{nam}}</tr>
</table>
</div>

2 Answers 2

2
  • Keep selected value as Boolean than String
  • In forEach, first argument is Object, access the model associated with it using name.selected
  • Initialize name1 array in ng-click handler

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.firstName = [{
    name: "John",
    selected: false
  }, {
    name: "Anil",
    selected: false
  }, {
    name: "Kumar",
    selected: false
  }];
  $scope.lastName = "Doe";
  $scope.addname = function() {
    $scope.name1 = [];
    angular.forEach($scope.firstName, function(name, selected) {
      if (name.selected) {
        $scope.name1.push(name)
      }
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <table>
    <tr ng-repeat="first in firstName">
      <td>
        <input type="Checkbox" ng-model="first.selected">{{first.name}}</td>
    </tr>
    <tr>
      <td>
        <input type="Button" ng-click="addname()" value="Submit" ng-model="lastName">
      </td>
    </tr>
    <tr ng-repeat="nam in name1">{{nam}}</tr>
  </table>
  {{name1}}
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName= [{name:"John",selected:false},{name:"Anil",selected:false},{name:"Kumar",selected:false}];
    $scope.lastName= "Doe";
    $scope.name1=[];
    $scope.addname=function(){
    angular.forEach($scope.firstName, function(name){
      if(name.selected === "true") {
        $scope.name1.push(name);
      }
});
 }
 });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<table >

<tr ng-repeat="first in firstName">
<td><input type="Checkbox" ng-model="first.selected"  ng-true-value="true" >{{first.name}}</td>

</tr>
<tr><td><input type="Button" ng-click="addname()" value="Submit" ng-model="lastName"></td></tr>
<tr ng-repeat="nam in name1"><td>{{nam}}</td></tr>

</table>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.