以下是代码结构,我使用热毛巾模板进行mvc项目。
脚本:
(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
来自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
始终显示为null,但我们在GetEmployeeInfo
方法中为其指定了一个值。因为无法获得状态。
请让我知道我们可以将数据导入vm.CountryCode
吗?
答案 0 :(得分:0)
(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;
}
}
})();
答案 1 :(得分:0)
如果您想使用EmployeeService.getEmpInfoForEdit和EmployeeService.getStates返回的数据,您应该在控制器中注入$ q并使用解析数据,如下例所示:
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);
}
}
答案 2 :(得分:0)
通过在GetStates
then
方法解决了该问题
var promises = [GetEmployeeInfo(),GetStates()];
common.activateController(promises, controllerId)
.then(function () { });
}
}
更改为
var promises = [GetEmployeeInfo()];
common.activateController(promises, controllerId)
.then(function () { GetStates() });
}
}