【发布时间】:2017-03-01 14:35:03
【问题描述】:
我正在尝试为应用设置一项功能,该功能将根据 URL 中的 id 参数显示来自 JSON 的给定 id 的内容。我在控制台 Module 'app' is not available! You either misspelled the module name or forgot to load it. 中收到错误 URL 应该是 url-to-site/app/{id#},并且通过在 JSON 中输入对象的 id#,视图应该更改为对应于该 id 的详细信息。
我目前有这个作为我的设置:
JS:
angular.module('app', ['ngRoute'])
.config(function ($routeProvider, $locationProvider) {
$routeProvider.when('/:id', {
templateUrl: 'index.html'
controller: 'appController'
});
})
.controller('appController', function($routeParams,$scope, $http) {
console.log($routeParams.id);
$scope.indexToShow = 0;
$http.get("shows.json")
.then(function(response) {
// console.log(response);
$scope.results = response.data;
});
$scope.changeNext = function(){
$scope.indexToShow = ($scope.indexToShow + 1) % $scope.results.length;
};
$scope.changePrevious = function(){
if($scope.indexToShow != 0){
$scope.indexToShow = ($scope.indexToShow - 1) % $scope.results.length;
}
};
});
HTML
<div ng-app="app">
<div ng-controller="appController">
...template code
</div>
</div>
JSON:
[
{
"id": 1,
"title": "Title 1",
"count": 54,
"image_location": "images/image-name.jpg"
}
]
【问题讨论】:
-
在您的 html 中添加
ng-view指令以加载路由模板。