4
我的AngularJS
應用程序具有item list
視圖(基本上是用戶可以從中選擇的大量項目列表)和item detail
視圖。我目前建立像這樣的路線:使用具有可變路線的AngularJS RouteParams
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/index',
controller: indexCtrl
}).
when('/itemDetail/:id', {
templateUrl: 'partials/itemDetail',
controller: itemDetailCtrl
}).
otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
}]);
function itemDetailCtrl($scope, $http, $routeParams) {
$http.get('/api/post/' + $routeParams.id).
success(function(data) {
$scope.post = data.post;
});
}
對於itemDetail/:id
路線,我想有一個以上的選項:這裏編號(例如「/:ID1 +:ID2 +:ID3」)。如果用戶選擇一個項目,兩個項目或更多項目,系統應該可以工作。
一個可能的解決方案,我還沒有完全能夠得到工作:
- 當用戶選擇幾個項目,我的應用程序日誌中的項目的數組
- 獨特
id
在當去細節視圖中,路線變爲'itemDetail/STRING
其中STRING
是由數組中的所有id
組成的字符串,它們之間有+
。例如:itemDetail/item1+item2+item3
- 在
itemDetailCtrl
,我走串,在+
符號拆分並創建一個新的數組,如 - 之前我然後使用
ng-repeat
採取這些ids
和我的數據庫中的信息加載到我的看法。
這是第一個想到的解決方案,但它似乎相當費力,並不特別優雅。有沒有更好的方法來實現這一點,如果是的話,我該如何更新itemDetailCtrl以反映多個ID?
你可以嘗試通過ID的一個「+」分隔的列表到:身份證,然後只是做: $ scope.ids = $ routeParams.id.split('+'); – zamber