express 和 angularJs $location.path 不重定向到 /

express and angularJs $location.path not redirecting to /

本文关键字:重定向 path location angularJs express      更新时间:2023-09-26

这是我在app.js文件中的AngularJs代码

 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('/')
     });
  }
}

这是我的快递服务器.js代码

   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'));
    });

这是我的详细信息.html代码

   <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>

这是我的 mongodb 后端服务

 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")  ;
            });
        }
    });
  }

我面临的问题是,当我按详细信息中的更新按钮时.html我可以在我的mongodb后端服务中更新详细信息。

在控制台中调用了 put 方法,但我无法使用 angularJs 应用程序中的 $location.path('/') 重定向到.js "/"路径?任何帮助将不胜感激。

根据文档,当浏览器 URL 更改时,$location 不会导致整个页面重新加载。要在更改 URL 后重新加载页面,请使用较低级别的 API,$window.location.href .

你需要创建一个控制器 ListCtrl,即使它什么都没有。您的 Web 检查器中可能出现错误,指出它找不到 ListCtrl。

看起来在您的 updateCar 函数中,您不会在成功时调用回调:

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.
        });
    }
  });
}

因此,您在调用 location.path 的编辑 ctrl 中的回调从未被调用。 :D