Javascript express和angularJs$location.path未重定向到/
这是我在app.js文件中的AngularJs代码Javascript express和angularJs$location.path未重定向到/,javascript,node.js,mongodb,angularjs,express,Javascript,Node.js,Mongodb,Angularjs,Express,这是我在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
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('/')
});
}
}
这是我的Express server.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'));
});
这是我的details.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>
我面临的问题是,当我在details.html中按下Update按钮时,我可以在我的mongodb后端服务中更新细节
在控制台中调用了put方法,但我无法使用angularJs app.js文件中的$location.path('/')将重定向到'/'路径?非常感谢您的帮助。根据,
$location
在更改浏览器URL时不会导致重新加载整个页面。要在更改URL后重新加载页面,请使用较低级别的API,$window.location.href
您需要创建一个控制器ListCtrl,即使其中没有任何内容。
您的web inspector中可能有一个错误,说它找不到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.
});
}
});
}
因此,在编辑ctrl中调用location.path的回调从未被调用:DHva您试图像这样在put方法的回调中放置参数:$scope.save=function(){CarsService.update({id:id},$scope.car,function(resp){$location.path('/')};}是添加了参数resp,运气不佳$window.location.href('/'))仍然没有重定向您是否尝试过纯Javascript
location.href
?如果这不起作用,那就是你在其他地方遇到了问题。请帮助我,我无法重定向?我应该把全部源代码都寄给你吗..在网络检查器中,我看到api/cars挂起,但数据库正在运行updated@shaunak1111$window.location.href是一个属性而不是一个方法。正确的使用方法是这样的<代码>$window.location.href='/'代码>。请参阅此相关问题的答案
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.
});
}
});
}