Javascript Angular-$routeParams.itemID用于加载domain.com/:itemID几乎正常工作?
我希望在路由中有参数,在变量名之前使用冒号Javascript Angular-$routeParams.itemID用于加载domain.com/:itemID几乎正常工作?,javascript,node.js,angularjs,express,mongoose,Javascript,Node.js,Angularjs,Express,Mongoose,我希望在路由中有参数,在变量名之前使用冒号 // dynamic pages for each ITEM, once selected // from $routeParams.itemID in ItemCtrl .when('/:itemID', { templateUrl: 'views/item.html', controller: 'ItemController' }) 单击div框时,Angular应路由到特定项 <div class="ite
// dynamic pages for each ITEM, once selected
// from $routeParams.itemID in ItemCtrl
.when('/:itemID', {
templateUrl: 'views/item.html',
controller: 'ItemController'
})
单击div框时,Angular应路由到特定项
<div class="itemBox" ng-click="getItem(item._id)">
但此错误记录在控制台中:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
...
at Promise.<anonymous> (/Users/Username/Downloads/project/v19/app/routes.js:41:8)
...
angular.module('ItemService')
.factory('Items', ['$resource', function($resource) {
return $resource('/api/items/:itemID', {
itemID: '@_id'
}, {
update: {
method: 'PUT'
}
});
}]);
虽然看起来问题可能在控制器中,因为所有其他API调用都可以工作。。所以我试着把$routeParams传遍了整个地方
angular.module('ItemCtrl', [])
// inject the Item service.factory into our controller
.controller('ItemController', function($scope, $routeParams, $http, Items, isEmptyObjectFilter) {
// get an Item after clicking it
$scope.getItem = function(id, $routeParams) {
Items.getOne(id, $routeParams)
// if successful getByID, call our function to get the Item data
.success(function(data, $routeParams) {
// assign our Item
$scope.item = data;
// for use with a parameter in appRoutes.js using itemID as the variable
$scope.itemID = $routeParams.itemID;
})
.error(function(data) {
console.log('Error: ' + data);
});
};
});
或者是服务?是否需要将$routeParams作为函数传递(id,$routeParams)
如果您能帮我调试一下,我将不胜感激。。感谢这条消息是因为您收到一个错误并执行res.send()方法,在您得到res.json()之后,express将尝试响应两次 尝试更改:
if (err)
res.send(err)
致:
角度资源示例:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
...
at Promise.<anonymous> (/Users/Username/Downloads/project/v19/app/routes.js:41:8)
...
angular.module('ItemService')
.factory('Items', ['$resource', function($resource) {
return $resource('/api/items/:itemID', {
itemID: '@_id'
}, {
update: {
method: 'PUT'
}
});
}]);
现在,您可以在控制器中执行此操作:
// Find
Items.get({
itemID: $routeParams.itemID
}, function(item) {
$scope.item = item;
});
// Update
$scope.item.name = 'New name';
$scope.item.$update();
// Remove
$scope.item.$remove();
看起来您获得的数据是正确的。问题是您希望在成功获得API调用后更改路由 $routeParams不会为您更改路线。这就是数据。使用$location更改路线
.controller('ItemController', function($scope, $routeParams, $location, $http, Items, isEmptyObjectFilter) {
$scope.getItem = function(id) {
Items.getOne(id)
.success(function(data) {
$scope.item = data;
$scope.itemID = $routeParams.itemID;
// redirect
$location.path('/' + $routeParams.itemID);
});
});
});
由于您的所有数据似乎都已准备就绪,您只需重定向到路由即可$位置应该是最好的选择。谢谢,再也没有错误了。。但是它不会路由到/:itemID——这是因为$routeParams在Ctrl中传递,还是我需要res.json(item);还是在某个地方?你必须使用角度资源而不是$http请求。不太确定这需要什么。。你有例子吗?是的,我修改了我的答案谢谢。。快到了。。它重定向到item.html页面,但是控制台在GET/api/items/534240001d3066cc11000002之后报告另一个GET/api/items。。你知道为什么它会执行另一个GetAll吗?你有没有一个主控制器在页面被点击时执行GetAll?或者ItemController中是否还有其他可以执行此操作的内容?因为当路由发生变化时,您将再次调用ItemController,所以其中的任何内容都将再次运行。啊,明白了。。那里也有一个将军。。将分离CTRL。。再次感谢!好极了!很高兴我能帮上忙。嗯,原来数据也有问题。。我们一直在测试各种东西,但是item.html没有被角度绑定填充,因为返回的数据是[object object],带有以下消息:
“Cast to ObjectId failed for value”[object object]“at path”\u id”“
有什么建议吗?
// Find
Items.get({
itemID: $routeParams.itemID
}, function(item) {
$scope.item = item;
});
// Update
$scope.item.name = 'New name';
$scope.item.$update();
// Remove
$scope.item.$remove();
.controller('ItemController', function($scope, $routeParams, $location, $http, Items, isEmptyObjectFilter) {
$scope.getItem = function(id) {
Items.getOne(id)
.success(function(data) {
$scope.item = data;
$scope.itemID = $routeParams.itemID;
// redirect
$location.path('/' + $routeParams.itemID);
});
});
});