I want to create a service that can be injected into different controllers for using an entity called Contact.
I have a .js file where everything is configured and I import the service there like this:
import ContactService from './service/ContactService';
import ContactsController from './ContactsController';
//...
angular.module(name,[
//...
]).service('contactService', ContactService)
.controller('contactsController', ContactsController)
//...
In ContactsController.js I want to use the service like this:
export default ($scope, contactService) => {
$scope.contacts = contactService.get();
}
Then in my ContactService.js I tried to do this, but it's not working
var Contact;
class ContactService {
constructor($resource) {
Contact = $resource('/contacts/:id');
}
get(id) {
return Contact.get(id);
}
get() {
return Contact.query();
}
//...
}
ContactService.$inject = ['$resource'];
export default ContactService;
How can I nicely implement ContactService.js so that it's working using the ES6 class and $resource?
this.Contact = $resource(...);and accessing it withreturn this.Contact;