我想通过php函数中的md-grid-list值动态设置md-select的值。请帮我。我的代码在这里。
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;
});
如果我这样跑,我发现以下错误...... TypeError:无法设置属性&#39; SalesOrganisations&#39;未定义的
console.log(&#39; second&#39;)首先显示在此部分之前
$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返回数据... 所以请帮助我!
答案 0 :(得分:1)
你在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
};
});
}
尝试这样的事情......
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);
}