Javascript 如何在函数之后调用函数
这是我的两个函数,我希望首先执行GetAllSLUDetails(),然后从controllerTa获取broadcase值。你能帮我吗Javascript 如何在函数之后调用函数,javascript,angularjs,Javascript,Angularjs,这是我的两个函数,我希望首先执行GetAllSLUDetails(),然后从controllerTa获取broadcase值。你能帮我吗 //==============call the state function after changed Country Name // get broadcase value from the controllerTa================= $scope.$on('evtTACountryCodeSelect', fu
//==============call the state function after changed Country Name // get broadcase value from the controllerTa=================
$scope.$on('evtTACountryCodeSelect', function (event, args) {
$scope.message = args.message;
var taCountryCode = $scope.message.split(",")[0];
var taStateCode = $scope.message.split(",")[1];
alert(taCountryCode);
alert(taStateCode);
GetAllSLUDetails(taCountryCode);
alert(taStateCode);
if (taStateCode != "") {
document.getElementById("ddlState").value = taStateCode;
}
});
//================To Get All Records ======================
function GetAllSLUDetails(CountryCode) {
// alert('ctrl State' + CountryCode);
var Data = stateService.getSLU(CountryCode);
Data.then(function (d) {
$scope.StateListUpdate = d.data;
//alert(d.data);
alert(JSON.stringify(d));
}, function () {
alert('Error');
});
}
请更好地解释您试图做什么,但通常您可以使用回调或承诺一个接一个地执行函数 因此,如果您想在GetAllSlueDetails之后执行某些操作,您可以:
$scope.$on('evtTACountryCodeSelect', function (event, args) {
GetAllSLUDetails(taCountryCode, function() { // THIS
// Do whatever
});
});
function GetAllSLUDetails(CountryCode, callback) {
// alert('ctrl State' + CountryCode);
var Data = stateService.getSLU(CountryCode);
Data.then(function (d) {
$scope.StateListUpdate = d.data;
//alert(d.data);
alert(JSON.stringify(d));
callback(); // THIS
}, function () {
alert('Error');
});
}
或使用承诺:
$scope.$on('evtTACountryCodeSelect', function (event, args) {
GetAllSLUDetails(taCountryCode).then(function() { // THIS
// Do whatever
});
});
function GetAllSLUDetails(CountryCode) {
return new Promise(function(resolve, reject) { // THIS
// alert('ctrl State' + CountryCode);
var Data = stateService.getSLU(CountryCode);
Data.then(function (d) {
$scope.StateListUpdate = d.data;
//alert(d.data);
alert(JSON.stringify(d));
resolve(d); // THIS
}, function (error) {
alert('Error');
reject(error); // THIS
});
});
}
作为
回调函数调用下一个函数;在函数GetAllSLUDetails(CountryCode)上,{}不存在working@AlexDCan你是拉小提琴还是弹琴?到底是什么不起作用?