Yep, that's the right way. I actually have a service called 'api' that
defines all my $resource objects and then returns an object that references
what I need the way I need it. For example:
//very simplified from what I really have
service('api', ['$http','$resource','$q',
function($http, $resource, $q){
var Account = $resource('/api/account',{},{ get: { method:
'get' } })
var Settings = $resource('/api/settings',{},{ read: { method:
'get' }, save: { method: 'put'} })
var Reports =
$resource('/api/reports/:reportName',{ reportName: '@ reportName' },{
loadReport: { method: 'get' }, update: { method: 'put'} })
return {
account: new Account(),
settings: new Settings(),
singleReport: function(){
return new Reports(); //case where I need an api
resource for managing a report that doesn't save state across controllers
it's injected into
}
}
}
])
//sample controller
function someController($scope, api){
$scope.settings = api.settings;
$scope.settings.$read( function success(dataReturned) {
console.log( 'is dataReturned really the same things as
$scope.settings? ', dataReturned == $scope.settings); //true
});
$scope.myReport = api.singleReport();
$scope.myReport.$loadReport({{ reportName: 'gross-sales' }) //will call
/api/reports/gross-sales and bind result to scope
}
Now my settings template in html will interpolate as soon as the api
returns the result and the same settings will be anywhere I inject the
service without needing to call $read again. There's even more magic you
can do, but this is a good start for now... good job getting this far and
questioning yourself, you are on the right path.
On Tuesday, March 18, 2014 6:36:10 AM UTC-6, John McPeek wrote:
>
> Hi,
> I need a list of reports in a couple different places so I extracted the
> $resource into a service and inject the service. I hold the data internal
> to the service. I would like to attached the service to $scope in each
> controller so binding will work. Am I crazy or is that the "right" way?
>
> Thanks,
>
> John
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.