Please bear with me. I'm very new to Angular—in fact only a day or so—but most of it makes sense to me. I've created Controllers, routes, etc. but putting it together I'm a bit lost.
My problem
I have a page with ng-view, and depending on the route different controllers are called with different data and templates. At the moment I have a "list" page and a "detail" page routing via an ID, but I need the list on both pages.
I read about using a factory and passing data one to the other, but is this the correct way?
plopApp.controller('juryCtrl', function($scope, $http) {
$http.get('/assets/javascripts/plop/plop/plopies.json').success(function(data) {
$scope.plopies = data;
});
});
plopApp.controller('plopDetailCtrl', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
$http.get('/assets/javascripts/plop/plop/' + $routeParams._id + '.json').success(function(data) {
$scope.plopor = data;
});
}]);
On the detail page, I want the list to be output as well. Do I do this using a factory, or have I structured the templates incorrectly?
UPDATE
I'm getting there - I understand the basics of the factories/services now, watched a video or two but am stuck on the execution for the detail page..
This is the factory Ive got working in my app
juryApp.factory('juryListFactory', function ( $http ) {
return {
list: function() {
return $http.get('/assets/javascripts/jury/jury/juries.json')
.then(
function(result) {
return result.data;
});
}
};
});
and now im trying to convert the inside of this controller:
juryApp.controller('juryDetailCtrl', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
$http.get('/assets/javascripts/jury/jury/' + $routeParams._id + '.json').success(function(data) {
$scope.juror = data;
});
}]);
to a factory.. currently I wrote this but it doesnt seem to work
juryApp.factory('juryListDetailFactory', '$routeParams', function($http, $routeParam){
var factory = {};
factory.getList = function(list, callback){
$http.get('/assets/javascripts/jury/jury/' + $routeParams._id + '.json').
success(function(data){
callback(data)
}).error(function(){
/*handle errors however you want */
callback(false);
})};
return factory;
}
I get this error Error: [ng:areq] Argument 'juryDetailCtrl' is not a function, got undefined
Anyhelp with juryDetailCtrl would be much appreciated