Answer the question
In order to leave comments, you need to log in
Why is ngRoute not working in angularJs?
Routing does not work
There is a module
var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
template: '<h1>Start</h1>',
})
.when('/pages/', {
templateUrl: 'pages.html'
})
.when('/pages/:pageId/', {
templateUrl: 'page.html',
controller:'pageCtrl'
})
.otherwise({
template:"404 no such page"
})
});
app.controller('pageCtrl', function($scope, $routeParams, pagesFactory) {
var pageId = Number($routeParams.postId);
$scope.page = _.findWhere(pagesFactory, {id: postId});
})
app.controller('pagesCtrl', function($scope, pagesFactory) {
console.log('pagesCtrl')
$scope.pages = pagesFactory
})
.run()
app.factory('pagesFactory',function(){
return [
$http.get('restaurant.json').success(function(data) {
$scope.countries = data;
})
]
})
<!DOCTYPE html>
<html ng-app="app">
<head>
<title>Рестораны</title>
<script src="node_modules/angular/angular.js"></script>
<script src="node_modules/angular-route/angular-route.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.2.0/lodash.js"></script>
<script type="text/javascript" src="main.js"></script>
<meta charset="utf-8" />
</head>
<body>
<a href="#/">1</a>
<a href="#/page">Page</a>
<ng-viev></ng-viev>
</body>
</html>
<h1>{{page.name}}</h1>
<h1>My pages</h1>
<div ng-repeat="page in pages">
<a href="#/pages/{{page.id}}">{{page.name}}</a>
</div>
[
{ "id": 1,
"name": "Челентано",
"address": "г.Симферополь, ул. Крымская 55",
"image": "chelin.jpg",
"kitchen":"Итальянская/Японская",
"description":"Лучший ресторан в Крыму",
"rating": 3.4
},
{ "id": 2,
"name": "Чао",
"address": "г.Симферополь, ул. Лермонтова 17 ",
"image": "chao.jpg",
"kitchen":"Итальянская/Японская",
"description":"Лучший ресторан в Крыму",
"rating": 4.1
},
{ "id": 3,
"name": "garris",
"address": "г.Симферополь, ул. Пушкина 2",
"image": "garris.jpg",
"kitchen":"Итальянская/Японская",
"description":"Лучшее кафе быстрого питания в Крыму",
"rating": 3.9
}
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question