使用服务AngularJS在控制器之间传递数据

时间:2017-01-13 08:46:46

标签: 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

调用GetCustomerData时,CustomerProfileController的值不会保留

enter image description here

1 个答案:

答案 0 :(得分:0)

要么你应该返回' http'服务对象,稍后使用回调处理then()函数中的响应。或者你应该传递一个回调函数来服务并在'然后'中运行该功能。稍后当你得到响应数据时运行。