Janani sridhar
Janani sridhar

Reputation: 23

Changes made to the global variable inside a function is not available to another function in angularjs

I have 2 scope functions and have declared one global scope variable. I have made the changes for the global variable in one function but it is not reflected in the other function in angularjs .

The scenario is like I have a checkbox in table and have written a function where values will be pushed to the $scope.selection=[]; if the checkbox is checked.

I have another delete button which is calling delete function and the delete operation will be performed with the value stored in the array.But I am getting the empty array instead of the modified one when I give console.log() inside the delete function. Kindly let me know your suggestions.

Code:

$scope.selectionlist = [];
$scope.selectitem = function(itemselected){ 
    if(itemselected.isChecked === true) {        
      $scope.selectionlist.push(itemselected.xyz); 
    } 
}
$scope.deletememo = function(){
    console.log("selection inside deletememo",$scope.selectionlist.length);      
}

Upvotes: 0

Views: 678

Answers (2)

balajivaishnav
balajivaishnav

Reputation: 2893

Though @denny jonh has answered your question, but still if you don't understand you can refer my answer too

JS

var app=angular.module("tableApp",[]);
app.controller("tblCtrl",function($scope){
  $scope.selectedItem=[];
  $scope.tableValues=[{
    "id":1,
    "name" : "dave",
    "country":"india",
    "designation":"software dev"
  },
  {
    "id":2,
    "name" : "giri",
    "country":"USA",
    "designation":"QA"
  },
  {
    "id":3,
    "name" : "dora",
    "country":"UK",
    "designation":"UI"
  }
  ]
  $scope.change=function(data){
    if(data.isChecked){
      $scope.selectedItem.push(data);
    }
    else{
      $scope.selectedItem.splice($scope.selectedItem.indexOf(data),1)
    }
  }
  $scope.displaySelectedRow=function(){
    if($scope.selectedItem==""){
      console.log("Select a row atleast")
    }
    else{
      console.log("The selected Row Value is: ",$scope.selectedItem);
    }
  }
})

Working Link

Upvotes: 1

Denny John
Denny John

Reputation: 464

Try this code.Run code snippet to see the result.

// Code goes here

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

app.controller('mainCntrl',function($scope){
  
  $scope.selectionlist = [];
  
  $scope.memo = [{'name': 'denny'},
                 {'name': 'john'},
                 {'name': 'joe'},
                 {'name': 'snowden'}
                 ];

  $scope.selectedItem = function(data){
    if(data.checkbox){
      $scope.selectionlist.push(data);
      console.log($scope.selectionlist);
      }
  else{
      var index = $scope.selectionlist.indexOf(data);
      $scope.selectionlist.splice(index, 1); 
      console.log($scope.selectionlist);
      }
    };

  $scope.deleteMemo = function(){
     console.log($scope.selectionlist);
   };
  
  });
<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@*" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    
 <div ng-app="myApp" ng-controller="mainCntrl">
    <table>
     <tr ng-repeat="x in memo">
      <td>{{ x.name }}</td>
      <td><input type="checkbox" ng-model="x.checkbox" ng-  change="selectedItem(x)"></td>
     </tr>
   </table>
 <button ng-click="deleteMemo()">Delete</button>
</div>
    
  </body>

</html>

Upvotes: 0

Related Questions