On Wednesday, April 2, 2014 10:16:20 PM UTC-4, Frank Seguin wrote:
>
> Hi, 
>
> I am trying to use $resource in a simple Todo example. I am able to create 
> a todo and it updates the page right away , but it also reload the whole 
> page.I have the same example with $http and it work fine. The problem, I 
> think,  is that I use $scope.todos = Todo.query(); to reload the data after 
> I create a new Todo. In the $http example, I use a the callback data from 
> the server instead. Anybody can help me? Thank you   
>
> *Here is my server side code:*
>
> app.get('/api/todos', auth, function(req, res) {
>
> // use mongoose to get all todos in the database
> Todo.find(function(err, todos) {
>
> // if there is an error retrieving, send the error. nothing after 
> res.send(err) will execute
> if (err)
> res.send(err)
>
> res.json(todos); // return all todos in JSON format
> });
> }); /*get todos*/
>
> app.post('/api/todos', auth, function(req, res) {
>
> // create a todo, information comes from AJAX request from Angular
> Todo.create({
> text : req.body.text,
> done : false
> }, function(err, todo) {
> if (err)
> res.send(err);
>
> // get and return all the todos after you create another
> Todo.find(function(err, todos) {
> if (err)
> res.send(err)
> res.json(todos);
> });
> }); 
>
> }); 
>
>
>
> *Here is the code I use with $resource: *
>
> app.factory('Todo', 
>  function($resource) {
>   return $resource('api/todos/:todos',{todos:'@todos'}) 
> });
>
> $scope.todos = Todo.query();
>
> $scope.createTodo=function(){
>
>
> Todo.save($scope.formData,function(){
>
> $scope.formData={};
> $scope.todos = Todo.query();
>
> });
>
> };
>    
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to