express 和 angularJs $location.path 不重定向到 /
express and angularJs $location.path not redirecting to /
这是我在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
相关文章:
- 通过javascript重定向html传递php变量
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 使用angular重定向到html页面
- 如何在chrome扩展中重定向到html页面
- 无法从jquery Mobile导航栏重定向到另一个页面
- ng视图外的链接重定向到ng视图内的页面
- 在phonegap应用程序内部重定向不起作用
- CORS-重定向到第二个GET正在接收的页面
- AJAX不会在文件上传后重定向到网页-POST方法
- 重定向时角度刷新浏览器
- 通过链接重定向不;我不在jstree中工作
- 根据select选项元素将表单重定向到不同的URL
- 通过JQuery重定向到另一个页面
- 重定向到“父窗口”
- 重定向iFrame中的父URL
- express 和 angularJs $location.path 不重定向到 /
- 如何在AngularJS中使用$location.path重定向转换之间的动画
- Angularjs中$location的重定向问题——无法读取property 'path'的定义
- Angularjs中$location的Url重定向问题-无法读取property 'path'的定义
- AngularJS$location.path(url)未重定向