$resource adds some things to the json string returned from the server?

347 views
Skip to first unread message

Thai

unread,
Dec 17, 2013, 10:58:29 PM12/17/13
to ang...@googlegroups.com
Hi All,

I'm learning angularjs 1.2.5 and this is what I notice:

html: <button ng-click="testJAXRS()">testing jax-rs with ng-click</button>

js:
var app = angular.module("myApp", ["ngResource"]);
app.controller("Controller", ["$scope", "$resource", function($scope, $resource) {
    $scope.testJaxRS = $resource("${pageContext.request.contextPath}/resource/test-jaxrs/something");

    $scope.testJAXRS = function() {
        var temp = $scope.testJaxRS.get(function() {
            alert(JSON.stringify(temp));
        });
    };
}]);


The json string returned from the server is {"key 1":["value 1 for key1","value 2 for key1"],"xyz":["abc","def","mnp","something"]}, but the alert prints out {"key 1":["value 1 for key1","value 2 for key1"],"xyz":["abc","def","mnp","something"],"$promise":{},"$resolved":true}. Is there any way to not have the "$promise" and "$resolved" keys in the variable temp?

Thanks.

Jeff Hubbard

unread,
Dec 17, 2013, 11:58:46 PM12/17/13
to ang...@googlegroups.com
Just curious, but why is this a problem? Also, angular.(to|from)Json handle this case for you by taking out $-prefixed properties.

LG Optimusv

unread,
Dec 18, 2013, 12:32:00 AM12/18/13
to ang...@googlegroups.com
Thanks, Jeff, for the angular.(to | from)Json. So, if I want to remove all the $-prefixed keys, I have to use something like angular.fromJson(angular.toJson(temp)), right?

This might be a problem if I want to display the content of the variable temp on the screen.


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/VhuN26UDCHY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Jeff Hubbard

unread,
Dec 18, 2013, 12:42:50 AM12/18/13
to ang...@googlegroups.com
You generally don't need to worry about this. The json filter removes those properties by default (I believe) and your view should know what properties it needs to display so it should never have to worry about those properties.

Michael Bielski

unread,
Dec 18, 2013, 10:36:25 AM12/18/13
to ang...@googlegroups.com
This is part of the changes to $resource. To remove those, change your resolution code from '.$then()' to '.$promise.then' and you'll get what you used to get.

jonr

unread,
Jan 7, 2014, 6:29:15 PM1/7/14
to ang...@googlegroups.com
Even when using '.$promise.then' you will still get the $promise and $resolved properties in 1.2.7. Is there a way to remove them for all resource calls? I don't want to have to write this everywhere:

Reply all
Reply to author
Forward
0 new messages