2017-01-13 63 views
0

我有一個控制器,它調用服務使用的手機號碼通行證控制器之間的數據使用服務AngularJS

adminApplication.controller('SearchByMobileController', 
          function ($scope, MobileSearchService) { 
    $scope.changed = function() { 
     if ($scope.result.length == 9) { 
      var PersonDetails = 
        MobileSearchService.GetPersonByMobileNumber($scope.result); 
     } 
    } 
}); 

還有一個控制器,以獲取結果集,以獲得病人的詳細信息。

adminApplication.controller('CustomerProfileController', 
           function($scope,MobileSearchService) { 
    $scope.show = 1; 
    $scope.tabvalue = function (val) { 
     $scope.show = val; 
    } 
    $scope.PatientDetails = MobileSearchService.GetCustomerData(); 
}); 

下面是我的服務

adminApplication.service("MobileSearchService", function ($http) { 
    var CustomerOutput = { 
     CustomerDetails : {}, 
    }; 

    var GetPersonByMobileNumber = function (enteredMobileNumber) { 
     $http.get('http://localhost:2095/api/User/Patients/' + 
       enteredMobileNumber).then(function successCallback(response) { 
      var output = []; 
      response.data.forEach(function (person) { 
       output.push(person.PatientId); 
       output.push(person.FirstName); 
       output.push(person.MiddleName); 
       output.push(person.LastName); 
       output.push(person.MaxCode); 
       output.push(person.Age); 
       output.push(person.Gender); 
      }); 
      var PatientId = output[0]; 

      $http.get('http://localhost:2095/api/User/PatientDetails/' 
       + PatientId).then(function successCallback(response) { 
       var output = {}; 
       if (response.data.length > 0) 
       { 
        output["Address"]= response.data[0].Address; 
        output["Age"] = response.data[0].Age; 
        output["DOB"] = response.data[0].DOB; 
        output["EmailId"] = response.data[0].EmailId; 
        output["EmrContactName"] = response.data[0].EmrContactName; 
        output["EmrContactNo"] = response.data[0].EmrContactNo; 
        output["EmrMailId"] = response.data[0].EmrMailId; 
        output["Gender"] = response.data[0].Gender; 
        output["ISVip"] = response.data[0].ISVip; 
        output["MaxId"] = response.data[0].MaxId; 
        output["Nationality"] = response.data[0].Nationality; 
        output["PatientId"] = response.data[0].PatientId; 
        output["PatientImage"] = response.data[0].PatientImage; 
        output["PatientName"] = response.data[0].PatientName; 
        output["PhoneNumber"] = response.data[0].PhoneNumber; 
        output["Relation"] = response.data[0].Relation; 
        output["Title"] = response.data[0].Title; 
       } 
       CustomerOutput.CustomerDetails = output; 
    enter code here 
       window.location.href = '/Account/CustomerProfile'; 
      }); 
     }, function errorCallback(response) { 
      return response.data; 
     });   
    } 

    var GetCustomerData = function() { 
     return CustomerOutput.CustomerDetails; 
    } 
    return { 
     GetPersonByMobileNumber: GetPersonByMobileNumber, 
     GetCustomerData: GetCustomerData 
    }; 

}); 

我的問題是,CustomerOutput.CustomerDetails值不是當我打電話使用CustomerProfileController

enter image description here

回答

0

要麼你應該返回'的GetCustomerData保留http'對象,然後在回調函數中處理then()函數中的響應。或者你應該傳遞一個回調函數來服務並在稍後獲得響應數據時在'then'函數中運行該函數。

相關問題