对函数内部的全局变量所做的更改不可用于angularjs中的另一个函数

时间:2016-12-20 09:53:41

标签: javascript angularjs

我有两个范围函数,并声明了一个全局范围变量。 我在一个函数中对全局变量进行了更改,但它没有反映在angularjs中的其他函数中。

该场景就像我在表格中有一个复选框,并编写了一个函数,如果选中该复选框,则值将被推送到$scope.selection=[];

我有另一个删除按钮,它正在调用删除功能,删除操作将使用存储在数组中的值执行。但是当我在console.log()内部给出$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); } 时,我得到的是空数组而不是修改过的数组。删除功能。请告诉我你的建议。

代码:

select itemgroupcode 
,      itemgroupdescription 
,      code 
,      description 
,      country 
,      postcodegebied 
,      name 
,      financialyear 
,      financialperiod 
,      sum(quantity) 
       aantal 
,      sum(amountdc) 
       omzet  
,      sum(quantity2jaar) 
       aantal2014 
,      sum(omzet2jaar) 
       omzet2014 
,      sum(quantity1jaar) 
       aantal2015 
,      sum(omzet1jaar)
       omzet2015 
,      sum(quantityhuidigejaar) 
       aantal2016 
,      sum(omzethuidigejaar)
       omzet2016
from   ( select substr(act.postcode, 1, 2) 
         ,       case 
                 when financialyear = year(getdate()) - 0
                 then amountdc 
                 else 0 
                 end
                 * -1 
                 omzethuidigejaar 
         ,       case 
                 when financialyear = year(getdate()) - 1 
                 then amountdc 
                 else 0 
                 end
                 * -1 
                 omzet1jaar
         ,       case 
                 when financialyear = year(getdate()) - 2 
                 then amountdc 
                 else 0 
                 end
                 * -1  
                 omzet2jaar
         ,       case 
                 when financialyear = year(getdate()) - 0
                 then quantity 
                 else 0 
                 end
                 quantityhuidigejaar
         ,       case 
                 when financialyear = year (getdate()) - 1 
                 then quantity 
                 else 0 
                 end
                 quantity1jaar 
         ,       case 
                 when financialyear = year(getdate()) - 2 
                 then quantity 
                 else 0 
                 end
                 quantity2jaar 
         ,       case 
                 when substr(act.postcode, 1, 2) >= '10'
                      and substr(act.postcode, 1, 2) < '20' 
                 then '1000-1999'
                 when substr(act.postcode, 1, 2) >= '20' 
                      and substr(act.postcode, 1, 2) < '30' 
                 then '2000-2999' 
                 when substr(act.postcode, 1, 2) >= '30' 
                      and substr(act.postcode, 1, 2) < '40' 
                 then '3000-3999' 
                 when substr(act.postcode, 1, 2) >= '40' 
                      and substr(act.postcode, 1, 2) < '50'
                 then '4000-4999' 
                 when substr(act.postcode, 1, 2) >= '50' 
                      and substr(act.postcode, 1, 2) < '60'
                 then '5000-5999' 
                 when substr(act.postcode, 1, 2) >= '60'
                      and substr(act.postcode, 1, 2) < '70' 
                 then '6000-6999' 
                 when substr(act.postcode, 1, 2) >= '70'
                      and substr(act.postcode, 1, 2) < '80'
                 then '7000-7999' 
                 when substr(act.postcode, 1, 2) >= '80' 
                      and substr(act.postcode, 1, 2) <= '89'
                 then '8000-8999' 
                 when substr(act.postcode, 1, 2) >= '90' 
                      and substr(act.postcode, 1, 2) <= '99'
                 then '9000-9999'
                 else 'unknown' 
                 end
                 postcodegebied
         ,       -1 * tle.amountdc
         ,       tle.financialperiod
         ,       tle.financialyear
         ,       act.country
         ,       act.name
         ,       itm.code
         ,       itm.description
         ,       tle.quantity
         ,       itm.itemgroupdescription
         ,       itm.itemgroupcode
         from    transactionlines tle
         join    exactonlinerest.crm.accounts act 
         on      act.code = tle.accountcode
         join    exactonlinerest.financial.glaccounts glt 
         on      glt.code = tle.glaccountcode
         --
         -- Type 110: grootboekrekening van het type omzet
         --
         and     glt.type = 110 
         join    exactonlinerest.logistics.items itm
         on      tle.itemcode = itm.code 
         --
         -- zodat er alleen transacties worden meegenomen die op een artikel geboekt zijn.
         --
         where   tle.itemcode is not null
       ) tle2
group 
by     itemgroupcode
,      itemgroupdescription 
,      code 
,      description 
,      country 
,      postcodegebied
,      name 
,      financialyear 
,      financialperiod
order 
by     itemgroupcode 
,      financialyear 
,      itemgroupdescription 
,      code 
,      description 
,      country 
,      postcodegebied
,      name 
,      financialperiod

2 个答案:

答案 0 :(得分:1)

虽然@denny jonh回答了你的问题,但如果你不明白,你也可以参考我的答案

<强> 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);
    }
  }
})

工作Link

答案 1 :(得分:0)

试试此代码。运行代码段以查看结果。

// 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>