Ram Mourya
Ram Mourya

Reputation: 2548

data from one angular call method to another method as parameter

The following is the code structure, iam using hot towel template for mvc project.

The script:

(function () {
'use strict';
var controllerId = 'EditEmployeeController';
angular.module('app').controller(controllerId, ['common', 'EmployeeService', EmployeeData]);

function EmployeeData(common, EmployeeService) {
var getLogFn = common.logger.getLogFn;
var log = getLogFn(controllerId);
var $filter = common.$filter;
var logError = common.logger.getLogFn('app', 'error');

var vm = this;   
vm.CountryCode;  
vm.Country = [];
vm.State = [];

vm.employeeInfo = {};

//calling the method to get the Employee info
activate();

//calling the methods to get the States
GetStates();

function activate() {

var promises = [GetEmployeeInfo(),GetStates()];
  common.activateController(promises, controllerId)
  .then(function () { });
 }
}

function GetEmployeeInfo() {
return EmployeeService.getEmpInfoForEdit(personId).then(function (data) {
vm.CountryCode = data.Country;
return vm.employeeInfo = data;
}


function GetStates() {

return EmployeeService.getStates(vm.CountryCode).then(function (data) {

return vm.State = data;
}

}
})();

EmployeeService.js

code snippet from EmployeeService.js

function getEmpInfoForEdit(personId) {
var EmpInfoForEdit = $resource('Employee/GetEmployeeDetailsForEdit', angular.fromJson(personId),     { 'query': { method: 'POST', isArray: false } });
var deferred = $q.defer();
EmpInfoForEdit.query({}, function (response) {
deferred.resolve(response);
}, function (error) {
deferred.reject(error);
})
return deferred.promise;
}

vm.CountryCode always shows null, though we are assigning a value to it in the GetEmployeeInfo method.Because unable to get the states.

please let me know can we get the data into vm.CountryCode ?

Upvotes: 0

Views: 574

Answers (3)

Ram Mourya
Ram Mourya

Reputation: 2548

the issue is resolved by moving the GetStates method inside the then

var promises = [GetEmployeeInfo(),GetStates()];
common.activateController(promises, controllerId)
.then(function () { });
}
}

changed to

var promises = [GetEmployeeInfo()];
common.activateController(promises, controllerId)
.then(function () { GetStates() });
}
}

Upvotes: 0

Bahaaldine Azarmi
Bahaaldine Azarmi

Reputation: 282

If you want to work with the EmployeeService.getEmpInfoForEdit and EmployeeService.getStates returned data, you should inject $q in your controller and use the resolve data as the following example :

angular.module('app').controller(controllerId, ['$q', 'common', 'EmployeeService', EmployeeData]);

...

function GetEmployeeInfo() {
    var deferred = $q.defer();
    return EmployeeService.getEmpInfoForEdit(personId).then(function (data) {
        deferred.resolve(data);
    }
}

function GetStates() {
    var deferred = $q.defer();
    return EmployeeService.getStates(vm.CountryCode).then(function (data) {
        deferred.resolve(data);
    }
}

Upvotes: 0

user3722785
user3722785

Reputation: 216

(function () {
'use strict';
var controllerId = 'EditEmployeeController';
angular.module('app').controller(controllerId, ['common', 'EmployeeService', EmployeeData]);

function EmployeeData(common, EmployeeService) {
var getLogFn = common.logger.getLogFn;
var log = getLogFn(controllerId);
var $filter = common.$filter;
var logError = common.logger.getLogFn('app', 'error');

var vm = this;   
vm.CountryCode=[];  
vm.Country = [];
vm.State = [];

vm.employeeInfo = {};

//calling the method to get the Employee info
activate();

//calling the methods to get the States
GetStates();

function activate() {

var promises = [GetEmployeeInfo(),GetStates()];
  common.activateController(promises, controllerId)
  .then(function () { });
 }
}

function GetEmployeeInfo() {
return EmployeeService.getEmpInfoForEdit(personId).then(function (data) {
vm.CountryCode = data.Country;
return vm.employeeInfo = data;
}


function GetStates() {

return EmployeeService.getStates(vm.CountryCode).then(function (data) {

return vm.State = data;
}

}
})();

Upvotes: 0

Related Questions