I have an application in which I upload a bunch of albums in one part, and then when the user clicks on each individual album, they get details for the album, such as songs, etc. Right now I am pulling out data from the database, but doing it twice, once in ProjectsCtrl to show all albums, and then again in ProjectDetailCtrl for individual album information. I feel that there is a faster way to do this, but I can’t figure out how to save the data the first time, to use the second controller again
(function() {
var app = angular.module('chp', ['ngRoute', 'projectControllers']);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/directory.html',
controller: 'ProjectsCtrl'
}).
when('/album/:slug', {
templateUrl: 'partials/album.html',
controller: 'ProjectDetailCtrl'
}).
otherwise({
redirectTo: '/'
});
}]);
var projectControllers = angular.module('projectControllers', []);
projectControllers.controller('ProjectsCtrl', ['$scope', '$http',
function ($scope, $http) {
$http.get('/get_albums').success(function(albums) {
$scope.projects = albums;
$scope.filters = { };
});
}]);
projectControllers.controller('ProjectDetailCtrl', ['$scope', '$http', '$routeParams', '$sce',
function($scope, $http, $routeParams, $sce) {
$http.get('/get_albums').success(function(albums) {
$scope.projects = albums;
for(var i = 0; i < $scope.projects.length; i++) {
if($scope.projects[i].slug === $routeParams.slug){
$scope.album = $scope.projects[i];
$scope.albumIdx = i;
break;
}
}
$scope.project = albums[$scope.albumIdx];
$scope.showVideo = function(id) {
var videoCode = $(this)[0].song.video;
$('#myModal .flex-video').html(videoCode);
$('#myModal .track-number').html('<span style="color: #f86081; display: inline-block; margin-right: 4px;">' + ($(this)[0].$index+1) + '.</span> ' + $(this)[0].song.title);
$('#myModal').foundation('reveal', 'open');
}
});
}]);
})();
source
share