Set value in md-select by md-grid-list with php calling function

I would like to set a dynamic value of md-select to the value of md-grid-list from a php function. Please help me. My code is here.

    appCity.controller('appCtrlCity', function($scope, $http, $mdDialog,  $timeout, $mdSidenav, $mdUtil, $log) {
          $scope.imagePath = 'img/yangon.png';
          $scope.toolbarIcon = 0;
          $scope.toolBarTitle = "Sales Dashboard";
          $scope.orgSelected = undefined;
          $http.post("controllers/loadMasterData.php?actionType=plant").success(function(data) {
            $scope.toolbarIcon = 0;
            $scope.toolBarTitle = "Sales Dashboard";
            json =JSON.stringify(data);
            json=JSON.parse(json)
            for (var i = 0; i < json.length; i++) {
              var jsonOrg = [];
              var CValue = json[i].CodeValue;
              var CDesc = json[i].CodeDesc;
              json[i].CodeRow = 1;
              json[i].CodeCol= 2;
              json[i].CodeColor = 'red';
              $http.post("controllers/loadMasterData.php?actionType=salesorg&plantType="+json[i].CodeValue).success(function(salesorg) {
                json[i].SalesOrganisations = {
                  'SalesOrg' : salesorg[0].SalesOrg,
                  'SalesOrgDesc' : salesorg[0].SalesOrgDesc
                };
console.log('first');
              });
            }
console.log('second');
            $scope.items = json;
          });

      

If I run like this, I caught the following error ... TypeError: Unable to set property "SalesOrganisations" to undefined

console.log ('second') is shown before this part

$http.post("controllers/loadMasterData.php?actionType=salesorg&plantType="+json[i].CodeValue).success(function(salesorg) {
        json[i].SalesOrganisations = {
          'SalesOrg' : salesorg[0].SalesOrg,
          'SalesOrgDesc' : salesorg[0].SalesOrgDesc
        };
        console.log('first');
      });

      

salesorg returns with data ... So help me!

+3


source to share


1 answer


You are missing a subtle and often misunderstood nuance with variable scoping in javascript.

for (var i = 0; i < json.length; i++) {
  var jsonOrg = [];
  var CValue = json[i].CodeValue;
  var CDesc = json[i].CodeDesc;
  json[i].CodeRow = 1;
  json[i].CodeCol= 2;
  json[i].CodeColor = 'red';
  $http.post("controllers/loadMasterData.php?actionType=salesorg&plantType="+json[i].CodeValue).success(function(salesorg) {
    // when you get to here, the loop has already finished, and i has been set to json.length
    json[i].SalesOrganisations = {
      'SalesOrg' : salesorg[0].SalesOrg,
      'SalesOrgDesc' : salesorg[0].SalesOrgDesc
    };
  });
}

      



Try something like this ...

for (var i = 0; i < json.length; i++) {
  var jsonOrg = [];
  var CValue = json[i].CodeValue;
  var CDesc = json[i].CodeDesc;
  json[i].CodeRow = 1;
  json[i].CodeCol= 2;
  json[i].CodeColor = 'red';
  // you need to pass this iteration value of i into a closure, so it maintained for the ajax handler
  (function(i){
      $http.post("controllers/loadMasterData.php?actionType=salesorg&plantType="+json[i].CodeValue).success(function(salesorg) {
        json[i].SalesOrganisations = {
          'SalesOrg' : salesorg[0].SalesOrg,
          'SalesOrgDesc' : salesorg[0].SalesOrgDesc
        };
      });
  })(i);

}

      

+1


source







All Articles