0

I have a situation where there are 3 check box ("initially checked"). Since it as 3 check box it will use ng-repeat for 3 time and loops the division for there time. Now once i uncheck any of the check box it should display div for 2 time. So far ,

$scope.items = [
    {id: 0, title: "apple",selected:true},
    {id: 1, title: "orange",selected:true},
    {id: 2, title: "grapes",selected:true},     
];

On ng-click in each check box i have called a function test("apple").

$scope.test = function(vals) {
    $scope.vl=[];
    for(var i=0;i<$scope.items.length;i++) {
        if($scope.items[i].title == vals) {
            console.log("dnt push");
        }
        else {
            $scope.vl.push({
                id: $scope.items[i].id,
                title: $scope.items[i].title,
                selected:true                      
            });
        }
    } 
    console.log("vl array");
    console.log($scope.vl);
}

Problem I am facing is it works fine for 1st uncheck but not when i do for multiple uncheck. When i check unchecked its not loading div.

In HTML I am using like,

<div ng-repeat="item in vl"> some tags </div>
1
  • what did you want to do precisly ? Commented Dec 15, 2015 at 6:46

2 Answers 2

2

not quite sure about your question, but hope my answer can give you some hints.

plunker demo

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';

  $scope.items = [{
    id: 0,
    title: "apple",
    selected: true
  }, {
    id: 1,
    title: "orange",
    selected: true
  }, {
    id: 2,
    title: "grapes",
    selected: true
  }];
});
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="[email protected]" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <p>Hello {{name}}!</p>
  <div ng-repeat="item in items">
    <input type="checkbox" ng-model="item.selected">{{item.title}}
  </div>
  {{items | json}}
</body>

</html>

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

Comments

0

If and only if you want to put in an array the item who are checked :

http://plnkr.co/edit/DvoPBBvKf3AhYM4qcBhx?p=preview

html

<div ng-repeat="item in items">
  <input type="checkbox" 
        ng-model="item.selected" ng-click="test(item.title)"> {{item.title}}
</div>

controller

$scope.test = function(vals) {
  $scope.vl=[];
  $scope.vlChecked=[];
  for(var i=0;i<$scope.items.length;i++) {
    if($scope.items[i].selected) {
      $scope.vlChecked.push({
        id: $scope.items[i].id,
        title: $scope.items[i].title,
        selected:true                    
      });
    }
  } 

  console.log("vlChecked array");
  console.log(JSON.stringify($scope.vlChecked));

}

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.