Javascript 位置URL参数错误解释
问题最初是试图正确设置url中的参数,但这篇文章解决了这个问题: 接着又出现了一个新问题。在控制器中设置url时Javascript 位置URL参数错误解释,javascript,angularjs,url,Javascript,Angularjs,Url,问题最初是试图正确设置url中的参数,但这篇文章解决了这个问题: 接着又出现了一个新问题。在控制器中设置url时 $scope.view = function(itemtmpl) { // change path and add parameter 'id' to the url $location.path('/itemtemplates/view/').search('id', itemtmpl.itemdefnid); // also tried with $rou
$scope.view = function(itemtmpl) {
// change path and add parameter 'id' to the url
$location.path('/itemtemplates/view/').search('id', itemtmpl.itemdefnid);
// also tried with $routeProvider
$location.url('/itemtemplates/view/' + itemtmpl.itemdefnid);
};
项的ID被解释为单独的参数,而不是一个数字(url将用于在工厂中进行REST调用,因此它必须是单个参数)
这是主要问题。我认为我的$location使用有问题,所以我去了AngularJS文档:$location 还是不走运 $routeParams变量打印输出表示参数已相应设置 我可能需要澄清的另一个想法是,为什么我的页面配置只会在$location.url刚刚附加到
view/1000
末尾时识别它,而不是如果它是一个参数view/?1000
,并且如果它有一个idview/?id=1000
.config(function($routeProvider) {
$routeProvider.when('/itemtemplates/view/:id', {
templateUrl: 'itemtemplates/add/main.tpl.html',
controller: 'ViewCtrl'
});
})
我会错过什么?提前感谢。在控制器中,对于我尝试访问的URL,工厂调用不正确。我必须指定我试图在REST调用中实现的参数,否则,参数将被松散地解释。在我的例子中,作为一个字符数组 是
$scope.itemtmpl = item.query($routeParams.id);
应该是
$scope.itemtmpl = item.query({id: $routeParams.id});
这解决了问题的前半部分。当使用.search()
设置参数时,我仍在试图弄清楚如何使用url