Search code examples
angularjsasp.net-mvc-4hottowel

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 ?


Solution

  • 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() });
    }
    }