2014-10-09 99 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。