Reputation: 325
I'm using dynamic routing in a section of a website where I'm displaying a list of items using ng-repeat. When somebody clicks on a particular item, its respective content is displayed on a new view.
This is my ng-repeat part of code:
<input type="text" placeholder="SEARCH" ng-model="search" />
<div id="track-list" ng-repeat="track in tracks | filter:search">
<a ng-href="#/radio/{{$index}}"><h3 class="track-list-title">{{track.title}}</h3></a>
<p class="track-list-date">{{track.date | date}}</p>
</div>
This is main index file:
<!DOCTYPE html>
<html ng-app="mainApp">
<head lang="en">
<meta charset="utf-8">
<title>Playlife Project</title>
<!-- styles -->
<link rel="stylesheet" href="css/bootstrap.css" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<!-- scripts -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28//angular-route.min.js"></script>
<script src="http://d2v52k3cl9vedd.cloudfront.net/plangular/3.1.0/plangular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<header>
<div id="logo"><a href="#/"><img src="img/logo.png" class="logo img-responsive" /></a></div>
<div id="nav" class="menu">
<ul>
<li class="menu-item"><a href="#/podcast">Podcast</a></li>
<li class="menu-item"><a href="#/record">Record Label</a></li>
<!--<li class="menu-item"><a href="#/live">Live Act</a></li>-->
<li class="menu-item"><a href="#/about">About</a></li>
<li class="menu-item"><a href="#/contact">Get In Touch</a></li>
<li class="menu-item"><a href="#">Shop</a></li>
<li class="mobile-menu-item"><i class="fa fa-2x fa-bars"></i></li>
</ul>
</div>
<div id="drawer" class="mobile-menu">
<ul>
<li><a href="#/podcast">Podcast</a></li>
<li><a href="#/record">Record Label</a></li>
<li><a href="#/live">Live Act</a></li>
<li><a href="#/contact">Get In Touch</a></li>
</ul>
</div>
</header>
<div ng-controller="homeController" class="{{pageClass}}" ng-view ></div>
<footer>
<div id="subscribe">
<form name="newsletter">
<input class="inp" type="email" name="email" id="email" placeholder="Be a part of the journey">
<input class="button" type="button" name="button" id="button" value="GET ON BOARD">
</form>
</div>
<div id="social">
<ul class="social-icons">
<li><a href="" target="_blank"><img src="img/facebook.png" /></a></li>
<li><a href="" target="_blank"><img src="img/soundcloud.png" /></a></li>
<li><a href="" target="_blank"><img src="img/twitter.png" /></a></li>
<li><a href="" target="_blank"><img src="img/youtube.png" /></a></li>
<li><a href="" target="_blank"><img src="img/instagram.png" /></a></li>
</ul>
</div>
</footer>
<script type="text/javascript" src="script/main.js"></script>
<script type="text/javascript" src="script/script.js"></script>
</body>
</html>
This is the main app:
var mainApp = angular.module("mainApp", ['ngRoute', 'ngAnimate', 'plangular']);
/* page routing */
mainApp.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html',
controller: 'homeController'
})
.when('/podcast', {
templateUrl: 'views/podcast.html',
controller: 'podcastController'
})
.when('/radio/:id', {
templateUrl: 'views/radio.html',
controller: 'radioController'
})
.when('/record', {
templateUrl: 'views/record.html',
controller: 'recordController'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'aboutController'
})
/*.when('/live', {
templateUrl: 'views/live.html',
controller: 'liveController'
})*/
.when('/contact', {
templateUrl: 'views/contact.html',
controller: 'contactController'
})
.otherwise({
redirectTo: '/'
});
})
/* player */
.config(function(plangularConfigProvider){
plangularConfigProvider.clientId = 'f39fd2f8db018982adef87c3ecd63e7a';
})
/* home controller */
.controller('homeController', function($scope, $timeout) {
$scope.pageClass = 'page page-home';
var INTERVAL = 5000, slides =[{head:"PLAY LIFE PROJECT", btn:'Listen to Playlife Podcast #002', link:'#', src:"./img/slide-1.jpg"},
{head:"SUBMIT YOUR TRACKS TO GET FEATURED ON PLAY LIFE RECORDS", btn:'Submit your track', link:'#', src:"./img/slide-2.jpg"},
{head:"PLAY LIVE CONNECT IS COMING NEAR YOU", btn:'Check Events', link:'#', src:"./img/slide-3.jpg"},
{head:"LISTEN TO PLAY LIVE PODCAST", btn:'Listen', link:'#', src:"./img/slide-4.jpg"}];
function setCurrentSlideIndex(index) {
$scope.currentIndex = index;
}
function isCurrentSlideIndex(index) {
return $scope.currentIndex === index;
}
function nextSlide() {
$scope.currentIndex = ($scope.currentIndex < $scope.slides.length - 1) ? ++$scope.currentIndex : 0;
$timeout(nextSlide, INTERVAL);
}
function loadSlides() {
$timeout(nextSlide, INTERVAL);
}
$scope.slides = slides;
$scope.currentIndex = 0;
$scope.setCurrentSlideIndex = setCurrentSlideIndex;
$scope.isCurrentSlideIndex = isCurrentSlideIndex;
loadSlides();
})
.controller('podcastController', ['$scope', function($scope) {
$scope.pageClass = 'page page-podcast';
$scope.tracks =[
{title:'Group Therapy 150 with Above & Beyond and Maor Levi', date: new Date(), link:'#'},
{title:'Group Therapy 151 with Above & Beyond and Martin Garrix', date: new Date(), link:'#'},
{title:'Group Therapy 152 with Above & Beyond and Grum', date: new Date(), link:'#'},
{title:'Group Therapy 153 with Above & Beyond and Andrew Bayer', date: new Date(), link:'#'},
{title:'Group Therapy 154 with Above & Beyond and Ilan Bluestone', date: new Date(), link:'#'},
{title:'Group Therapy 155 with Above & Beyond and Armin Van Buuren', date: new Date(), link:'#'},
{title:'Group Therapy 156 with Above & Beyond and Hardwell', date: new Date(), link:'#'}
]
}])
.controller('radioController', ['$scope', '$routeParams', function($scope, $routeParams) {
$scope.pageClass = 'page';
$scope.track=$scope.tracks[ $routeParams.id]
}])
.controller('recordController', function($scope) {
$scope.pageClass = 'page page-record';
})
.controller('aboutController', function($scope) {
$scope.pageClass = 'page-about';
})
/*
mainApp.controller('liveController', function($scope) {
$scope.pageClass = 'page-live';
});
*/
.controller('contactController', function($scope) {
$scope.pageClass = 'page page-contact';
});
The items are displayed properly when when I click on them, they redirect to this new view but it does not show the title, all I see is {{track.title}}
.
On checking in Google Chrome's console, I'm getting this error:
TypeError: Cannot read property '4' of undefined
at new <anonymous> (http://localhost:63342/New%20website/script/main.js:97:31)
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:34:265)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:34:394)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:66:112
at link (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28//angular-route.min.js:7:248)
at J (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:53:345)
at f (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:46:399)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:46:67
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:47:303
at u (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:51:28) <div ng-controller="homeController" class="{{pageClass}} ng-scope" ng-view="">
Upvotes: 0
Views: 134
Reputation: 1427
The problem is $scope.tracks
is defined in podcastController and you're accessing it in radioController which doesn't know about podcastController. You can either nest radioController in podcastController or move the initialization of $scope.tracks
to homeController.
Try moving
$scope.tracks =[
{title:'Group Therapy 150 with Above & Beyond and Maor Levi', date: new Date(), link:'#'},
{title:'Group Therapy 151 with Above & Beyond and Martin Garrix', date: new Date(), link:'#'},
{title:'Group Therapy 152 with Above & Beyond and Grum', date: new Date(), link:'#'},
{title:'Group Therapy 153 with Above & Beyond and Andrew Bayer', date: new Date(), link:'#'},
{title:'Group Therapy 154 with Above & Beyond and Ilan Bluestone', date: new Date(), link:'#'},
{title:'Group Therapy 155 with Above & Beyond and Armin Van Buuren', date: new Date(), link:'#'},
{title:'Group Therapy 156 with Above & Beyond and Hardwell', date: new Date(), link:'#'}
]
into homeController and update
<div ng-controller="homeController" class="{{pageClass}}" ng-view ></div>
to
<div ng-controller="homeController" class="{{pageClass}}">
<div class="{{pageClass}}" ng-view ></div>
</div>
Upvotes: 1
Reputation: 867
Very simple, scope.tracks isn't defined in radioController. The error message is telling you to look into line 97. Pretty sure that if you just replace $scope.track with dummy data, your code will work.
Upvotes: 0
Reputation: 582
If it's actually outputting the curly braces onto the page, that's usually a signal that you're missing a dependency somewhere.
Check the header of your index.html to make sure your controllers, services, factories, etc are all being loaded: check file names, structure (subfolders), etc. Also check spelling, capitalization, etc, just to be sure. The missing injection is probably in there somewhere. :)
Upvotes: 0