Question

I need to get an inserted object immediately after insertion has been done. But this is a problem with Angularjs $resource.

module.factory('SearchQueries', function($resource){         
  return $resource('/instances/searches/:_id', {_id: '@id'});
});

var new_search = new SearchQueries({query: query});

new_search.$save(function(new_query){
  console.log(new_query);
  console.log(new_query.id, new_query.query);                           
  console.log(new_query.$get()); // This is undefined                   
  $scope.searchQueries.push(new_query);
});                                                

Server response:

{"query": "inserted text", "id": 53}                                                   

new_query printed in a console:

h {query: "f7b1b2214bda4aa29b7ef5029c918590", id: 53, $get: function, $save: function,     $query: function…}
$$hashKey: "01J"
id: 53
query: "inserted text"

new_query.id, new_query.query printed in a console:

53 "f7b1b2214bda4aa29b7ef5029c918590"

Instead of inserted text I have some kind of hash. By the way, what the hash is this?

In the template a bit of millisecond a new_query is a hash, after $get is done (success) the new_query is inserted text. How to get inserted text directly after $get successed?

Was it helpful?

Solution

new_query is a SearchQueries instance with the data response from the server. It seems like your server responds with {query: "f7b1b2214bda4aa29b7ef5029c918590", id: 53}, this is not created by Angular. The $ and $$ properties are from the SearchQueries prototype. If you want to print the result from new_query.$get() you should do it in a callback function:

new_query.$get(function(result){
  console.log(result);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top