Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 从一个角度调用方法到另一个方法的数据作为参数_Angularjs_Asp.net Mvc 4_Hottowel - Fatal编程技术网

Angularjs 从一个角度调用方法到另一个方法的数据作为参数

Angularjs 从一个角度调用方法到另一个方法的数据作为参数,angularjs,asp.net-mvc-4,hottowel,Angularjs,Asp.net Mvc 4,Hottowel,下面是代码结构,iam使用热毛巾模板进行mvc项目 剧本: EmployeeService.js function getEmpInfoForEdit(personId) { var EmpInfoForEdit = $resource('Employee/GetEmployeeDetailsForEdit', angular.fromJson(personId), { 'query': { method: 'POST', isArray: false } }); var deferre

下面是代码结构,iam使用热毛巾模板进行mvc项目

剧本:

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;
}
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吗?

如果您想使用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);
    }
}

通过将GetStates方法移动到then中,可以解决此问题

改为

var promises = [GetEmployeeInfo()];
common.activateController(promises, controllerId)
.then(function () { GetStates() });
}
}
我们在EmployeeService中使用$q,我们可以在GetEmployeeInfo方法中获得响应,但在GetState中,变量vm.CountryCode始终为null。
var promises = [GetEmployeeInfo(),GetStates()];
common.activateController(promises, controllerId)
.then(function () { });
}
}
var promises = [GetEmployeeInfo()];
common.activateController(promises, controllerId)
.then(function () { GetStates() });
}
}