【发布时间】:2017-02-02 15:51:19
【问题描述】:
我的控制器上有这个功能:
// Get a specific Parte
partesc.getParte = function (id) {
var url = endpointApiURL.url + "/fabricante/" + id;
$scope.PartesPromise = $http.get(url)
.then(function (response) {
partesc.parte= response.data;
})
.catch(function (error) {
console.log(error);
if (error.status == '412') {
console.log('Error obteniendo datos: ' + error.data.error);
}
});
}
我还有第二个功能:
partesc.openEdit = function(id) {
partesc.getParte(id);
console.log(partesc.parte); }
我从前端的按钮调用 openEdit 函数。所以 console.log 部分打印未定义。我认为这不是在等待调用函数 getParte(id) 的响应。
我怎样才能让它等待函数的响应来打印结果?我做错了?
更新 1
console.log 仅供参考。我需要使用在另一个函数(openEdit)中返回另一个函数(getParte)的数据
解决方案
感谢我在这里接受的答案,我找到了解决方案。
// Get a specific Parte
partesc.getParte = function (id) {
var url = endpointApiURL.url + "/fabricante/" + id;
return $http.get(url)
.then(function (response) {
partesc.parte= response.data;
})
.catch(function (error) {
console.log(error);
if (error.status == '412') {
console.log('Error obteniendo datos: ' + error.data.error);
}
});
}
partesc.openEdit = function(id) {
$scope.PartesPromise = partesc.getParte(id)
.then(function() {
console.log(partesc.parte);
});
}
谢谢
【问题讨论】:
-
你能从这里登录吗? $http.get(url) .then(function (response) { partesc.parte= response.data; console.log(partesc.parte); })
-
你可以从函数本身返回promise,然后在返回的promise对象上使用.then(successCallback)。
-
@Groben 是的...这带来了一个带有数据的对象。
-
你想做什么?
-
我更新了我的答案,请参阅更新 1 我的朋友@Groben 谢谢
标签: angularjs function http response