2014-10-09 102 views
0

问题原本是想正确地设置在URL中的参数,但是,这篇文章解决了这个问题:How can I set a query parameter in AngularJS without doing a route?位置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 $routeProvider 
    $location.url('/itemtemplates/view/' + itemtmpl.itemdefnid); 
}; 

商品的ID被解释为独立的参数,而不是一个号码(URL将被用来制造工场的REST调用,所以它必须是一个参数)。

Chrome Console Log

enter image description here

这是首要的问题。


我觉得有我的$ location使用量的问题,所以我去了AngularJS文档:https://docs.angularjs.org/api/ng/service/ $位置

仍然没有运气。

$ routeParams变量打印输出表示相应地设置参数。

我可能需要澄清的另一个想法是,为什么我的网页的配置将只承认时,它只是添加到末尾view/1000,而不是如果它是一个参数view/?1000的$ location.url,仍然它是否有一个ID将不会进行view/?id=1000

.config(function($routeProvider) { 
    $routeProvider.when('/itemtemplates/view/:id', { 
    templateUrl: 'itemtemplates/add/main.tpl.html', 
    controller: 'ViewCtrl' 
    }); 
}) 

我能错过什么?提前致谢。

回答

0

在我试图去的URL控制器中,工厂调用不正确。我必须指定我试图在REST调用中实现的参数,否则,参数将被松散地解释。在我的情况下,作为一个字符数组。

是......

$scope.itemtmpl = item.query($routeParams.id); 

本来应该...

$scope.itemtmpl = item.query({id: $routeParams.id}); 

这解决了问题的前半部分。当我使用.search()设置参数时,我仍然试图弄清楚如何使用这个url。