0

Here is my AngularJs code in app.js file

 var CarApp = angular.module('CarApp',['ngResource'])

 CarApp.config(function($routeProvider){
    $routeProvider
       .when('/',{controller:ListCtrl,templateUrl:'partials/list.html'})
       .when('/edit/:id',{controller:EditCtrl,templateUrl:'partials/details.html'})
       .otherwise({redirectTo:'/'})

  });

  // to map update method
  CarApp.factory('CarsService',function($resource){
       return $resource('/api/cars/:id',{id:'@_id'} , {update:{method:'PUT'}})
  });

  function EditCtrl($scope,$location,$routeParams,CarsService){

 var id = $routeParams.id;

 //console.log(id);

 CarsService.get({id: id},function(resp){
    $scope.car = resp;

 });

 // Update Page title
 $scope.action = "Update";

  $scope.save = function() {
     CarsService.update({id:id},$scope.car,function(){
        $location.path('/')
     });
  }
}

Here is my Express server.js code

   var express = require('express');
   var http = require('http');
   var path = require('path');
   var cars = require('./server/api/cars.js')

   var app = express();

   var client_dir =  path.join(__dirname, '/client')

   // all environments
    app.set('port', process.env.PORT || 3000);
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(app.router);
    app.use(express.static(client_dir));
    app.use(express.static(path.join(__dirname, '/image')));


    app.get('/', function(req,res){
       res.sendfile(path.join(client_dir,'index.html'))

    });

    // ordering is important to for angularJs to differentiate between list all  and read

    app.get('/api/cars',cars.list);

    app.get('/api/cars/:id',cars.read);
    app.post('/api/cars/',cars.create);
    app.put('/api/cars/:id',cars.update);
    app.del('/api/cars/:id',cars.delete);

    http.createServer(app).listen(app.get('port'), function(){
       console.log('Express server listening on port ' + app.get('port'));
    });

Here is my details.html code

   <h2>{{action}} Ferrari</h2>

  <form name="carform" class="form-horizontal">

      <div class="control-group">
           <label class="control-label" for="year">Year</label>
           <div class="controls">
              <input type="text" ng-model="car.year" id="year" name="year" placeholder="">
           </div>
      </div>

<div class="form-actions">
    <button ng-click="save()" class="btn btn-primary">
        Update
       </button>
      <a href="/" class="btn">Cancel</a>
   </div>
 </form>

Here is my mongodb backend service

 function update(req,res){

var newCarData = req.body;
var id = req.params.id;

newCarData._id = ObjectId(id);
updateCar(newCarData,function(err){
     if(err) throw new Error("unable to update");

     else{
        res.json();
     }
  });
}


 function updateCar(car,callback){
    db.collection(collectionName,function(error,collection){
        if(error) callback(true);

        else {
            collection.save(car,function(){
                //console.log("updated data")  ;
            });

        }
    });
  }

The problem i am facing is when i press Update button in details.html i can update the details in my mongodb backend service.

In the console the put method is called but i cannot redirect to the '/' path using $location.path('/') in angularJs app.js file ? Any help will be highly appreciated.

shaunak1111
  • 951
  • 1
  • 11
  • 17
  • Hva you tried to put a paramaeter in the callback of the put method like this : $scope.save = function() { CarsService.update({id:id},$scope.car,function(resp){ $location.path('/') }); } – Thomas Pons Sep 21 '13 at 07:46
  • yes added parameter resp , no luck still not working – shaunak1111 Sep 21 '13 at 07:50

3 Answers3

2

As per the docs, $location does not cause a full page reload when the browser URL is changed. To reload the page after changing the URL, use the lower-level API, $window.location.href.

WispyCloud
  • 4,140
  • 1
  • 28
  • 31
  • $window.location.href('/') still not redirecting – shaunak1111 Sep 21 '13 at 08:15
  • Have you tried plain Javascript `location.href`? If that does't work, it's that you have an issue somewhere else. – WispyCloud Sep 21 '13 at 08:17
  • please help me i am not able to redirect? shall i mail u the entire source code.. – shaunak1111 Sep 21 '13 at 11:52
  • In the network inspector i see put api/cars pending but the database is being updated – shaunak1111 Sep 21 '13 at 12:33
  • @shaunak1111 $window.location.href is a property and not a method. The proper way of using it will be something like this; `$window.location.href = '/';` . See the answer to this related Stackoverflow question http://stackoverflow.com/questions/7077770/window-location-href-and-window-open-methods-in-javascript – dantheta Mar 28 '14 at 10:30
0

You need to create a controller ListCtrl, even if it's got nothing in it. You probably have an error in your web inspector saying it can't find ListCtrl.

richardw
  • 196
  • 1
  • 5
0

It looks like in your updateCar function, you are not calling your callback on success try:

function updateCar(car,callback){
  db.collection(collectionName,function(error,collection){
    if(error) callback(true);

    else {
        collection.save(car,function(){
            //console.log("updated data")  ;
            callback(null, { ok: true }); //something like this.
        });

    }
  });
}

So your callback in the edit ctrl that calls the location.path, was never being called. :D

JoelV
  • 85
  • 1
  • 8