我试图使用REST API交互$资源与下面的代码:
厂内:
api.user = $resource(api.baseUrl + '/admin/login', {}, {
login: {method:'POST'}
});
控制器的一个:
vm.user.$login().$promise.then(function(successResult) {
console.log(successResult);
}, function(errorResult) {
console.log(errorResult);
if(errorResult.status === 404) {
}
});
我得到的错误:“类型错误:由于$ promise未定义为资源对象,因此无法读取“未定义”属性。
Question 1: I read on multiple questions that one can use the $promise property to write the callbacks. What am I doing wrong here that the $promise property is coming as undefined for login method on user object.
控制器二:
vm.user.$login(function(successResult) {
console.log(successResult);
}, function(errorResult) {
console.log(errorResult);
if(errorResult.status === 404) {
}
});
这妥善处理成功/错误处理,但是successResult对象上,有$承诺的两个附加属性:未定义,$解决:真实的,而我假设successResponse应该是服务器返回的实际普通对象,但它看起来像是$ resource的一个实例。
Q2: Is there a way to capture the plain object returned by the server while using the $resource, not having $resource properties?
任何有助于解决困惑的赞赏。
根据角度文档,$ resource retu rns一个对象,而在ControllerOne中,你试图得到'promise' – batmaniac7
$资源返回了一个对象,我分配给api.user,然后我打电话给$ login方法,根据我的理解(不正确可能是)应该返回一个资源对象和一个有效的$ promise属性,而它是未定义的。我希望这可以稍微澄清一点。 – user1242321
@ user1242321,你能看看我的答案吗? – developer033