2015-09-07 61 views
-2

這裏是我的兩個函數,我想首先執行GetAllSLUDetails(),然後從controllerTa獲取廣義值。所以,能不能幫我如何調用函數後的角度

//==============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'); 
      }); 

     } 
+0

呼叫下一個功能爲'callback'功能。 –

回答

0

請解釋更好的你正在嘗試做的,但一般可以達到另一個使用回調或承諾後執行的功能。

所以,如果你想您可以將GetAllSLUDetails後執行的東西:

$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 
     }); 
    }); 
} 
+0

Here alert('ctrl State'+ CountryCode);在 功能GetAllSLUDetails(COUNTRYCODE){ } 不工作@ AlexD – SoloThink

+0

,你可以做一個小提琴或plunkr?什麼不完全正確? – AlexD