admin管理员组文章数量:1335615
I'm trying to get the response of a request using $resource
, for example I have:
angular.module('app').factory('AuthResource', ['$resource', function($resource) {
return {
isAuthenticated : function() {
return $resource('/api/v1/auth/authenticated').query();
}
}
}]);
Then in my controller I'm calling this service and doing:
console.log(AuthResource.isAuthenticated());
This doesn't return the actual result, which is simply a single object {'success' : 'true'}
.
Instead it returns:
Resource {$resolved: false, $then: function, $get: function, $save: function, $query: function…}
$resolved: true
$then: function (callback, errback) {
success: false
__proto__: Resource
How do I go about getting the actual returned object? I'm not applying this to any models, just using the data to determine some routing.
Thank you!
I'm trying to get the response of a request using $resource
, for example I have:
angular.module('app').factory('AuthResource', ['$resource', function($resource) {
return {
isAuthenticated : function() {
return $resource('/api/v1/auth/authenticated').query();
}
}
}]);
Then in my controller I'm calling this service and doing:
console.log(AuthResource.isAuthenticated());
This doesn't return the actual result, which is simply a single object {'success' : 'true'}
.
Instead it returns:
Resource {$resolved: false, $then: function, $get: function, $save: function, $query: function…}
$resolved: true
$then: function (callback, errback) {
success: false
__proto__: Resource
How do I go about getting the actual returned object? I'm not applying this to any models, just using the data to determine some routing.
Thank you!
Share Improve this question asked Apr 24, 2013 at 16:42 dzmdzm 23.6k51 gold badges152 silver badges229 bronze badges 1- check out [this answer][1] , it helped me out [1]: stackoverflow./a/16197255/1505895 – paynestrike Commented Jun 28, 2013 at 1:34
3 Answers
Reset to default 3I set up a test like this:
var status = {};
$httpBackend.expectGET("/api/Accounts/AuthenticationStatus").respond(status);
Then I had an expectation:
expect(actual).toBe(status);
I was getting the following error:
Expected { $resolved : true, $then : Function } to be { }.
After scratching my head for a long time, I finally realized that the object returned by the get() function was never going to be exactly the same object I set up the $httpBackend service to respond with, but that it would return the base object { $resolved : ..., $then : ... } and, when resolved, add the additional fields included with my response object to that object.
Hope that makes better sense than the previous poster.
Just Modify you code as below
angular.module('app').factory('AuthResource', ['$resource', function($resource) {
return {
isAuthenticated : function() {
return $resource('/api/v1/auth/authenticated')
}
}
}]);
--controller
AuthResource.isAuthenticated().query(function(data){
console.log(data);
});
When the data is returned from the server then the object is an instance of the resource type and all of the non-GET methods are available with $ prefix
Can you base a solution on the answer here https://stackoverflow./a/11856710/1371408. Something like:
AuthResource.isAuthenticated(
{}, // params (ie. none)
function (data) { // success callback
// do what you want with values returned from successful request, contained in 'data'
},
function (error) {
console.log(error); // Error details
}
);
本文标签: javascriptAngularjs getting value of response using resourceStack Overflow
版权声明:本文标题:javascript - Angularjs getting value of response using $resource - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742389263a2465661.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论