3
votes

I'd like to set dynamically md-select's value by md-grid-list value from 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 following error ... TypeError: Cannot set property 'SalesOrganisations' of undefined

console.log('second') show first 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 return with data... So Pleae Help me!

1

1 Answers

1
votes

You're missing a subtle and often-misunderstood nuance with variable scope 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 instead ...

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's value of i into a closure, so it's 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);

}