I have an app where I am loading a bunch of albums in one partial and then when a user clicks on each individual album, they get details for the album like songs, etc. Right now I am pulling the data from database but doing it twice, once in ProjectsCtrl to show all the albums and then again in ProjectsDetailCtrl for the individual album info. I feel like there is a faster way to do this, but can't figure out how to save the data from the first time to use again in the 2nd controller
(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;
// videoCode = '<iframe width="560" height="315" src="'+video+'" frameborder="0" allowfullscreen></iframe>';
$('#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');
}
});
}]);
})();