What I would like to do is to create a model (e.g Book) in angular with ES6 that can be instantiated with some arguments (new Book('someTitle')). I have got it working with the following (suppose we need to inject something, such as $http):
function foo($http) {
return class Book {
constructor(title) {
this.title = title;
}
fetch() {
return $http.get('api/book/' + this.title);
//...
}
};
}
angular.module('app')
.service('Book', ['$http', function($http) {
return foo($http);
}];
The problem is this doesn't seem like a clean way of doing things. Also I'm not sure what problems could come out of it. Could anyone suggest a better way to achieve the same thing?
foofunction? You could just return the class in the factory itself. That would make it more clear. This is how it's done in angular1. And by the way I dont really see how this question relates to es6. It would be the same using es5 classes.