我試圖從瀏覽器中的URL傳遞參數給控制器,例如,應該在控制器中傳遞$scope.id
,所以JSON加載是這http://52.41.65.211:8028/api/v1/categories/122/books.json
,雖然沒有工作,任何想法?
URL例如 http://www.example.com/categories/122
app.js
...
.when('/categories/:categoryId', {
controller: 'BookCategoryController',
templateUrl: 'views/booksincategory.html'
})
...
控制器
app.controller('BookCategoryController', ['$scope', 'bookcategories', '$routeParams', function($scope, bookcategories, $routeParams) {
bookcategories.getAllBooks($scope.id).then(function(response) {
$scope.detail = response.data.books[$routeParams.categoryId];
});
}]);
服務
app.service('bookcategories', ['$http', function($http) {
return {
getAllBooks: function(id) {
return $http.get('http://52.41.65.211:8028/api/v1/categories/'+ id + '/books.json')
}
}
}]);
booksincategory.html
<div class="category col" ng-repeat="book in detail">
<h3 class="title">{{book.title}}</h3>
</div>
你有機會看看[這個答案](https://stackoverflow.com/a/46216131/2435473)?我不明白你爲什麼要打開多個問題..而不是專注於單個和修復它在一個地方:) –