I'm having problems accessing Angular built-in services such as $http when creating a service with ES6.
For example, I'm creating a "ResultsFinder" service that will do an AJAX call and then do some stuff. The problem is that I only have access to $http on the constructor (if I pass it as an argument), but not on other methods (such as getResults).
See this code example:
(() => {
'use strict';
class ResultsFinder {
constructor($http) {}
getResults() {
return 'ResultsFinder';
}
}
/**
* @ngdoc service
* @name itemManager.service:ResultsFinder
*
* @description
*
*/
angular
.module('itemManager')
.service('ResultsFinder', ResultsFinder);
}());
Inside getResults I don't have access to $http. In order to have access I should do something that I don't feel right like this:
(() => {
'use strict';
class ResultsFinder {
constructor($http) {
this.$http = $http;
}
getResults() {
// Here I have access to this.$http
return 'ResultsFinder';
}
}
/**
* @ngdoc service
* @name itemManager.service:ResultsFinder
*
* @description
*
*/
angular
.module('itemManager')
.service('ResultsFinder', ResultsFinder);
}());
Anyone can give me some advice about the proper way to handle this?
We're particularly excited to see AngularJS embrace ECMAScript 6, part of which includes replacing its own module system with the ES6 module system. The ES6 modules spec has been finalised, and it will now begin to be introduced to browsers, although we can't expect full support for a while yet.
AngularJS services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app. AngularJS services are: Lazily instantiated – AngularJS only instantiates a service when an application component depends on it.
AngularJS provides many inbuilt services. For example, $http, $route, $window, $location, etc. Each service is responsible for a specific task such as the $http is used to make ajax call to get the server data, the $route is used to define the routing information, and so on.
You need to use this.$http
inside your getResults
method.
(() => {
'use strict';
class ResultsFinder {
static $inject = ['$http'];
constructor($http) {
this.$http = $http;
}
getResults() {
return this.$http.get('/foo/bar/');
}
}
/**
* @ngdoc service
* @name itemManager.service:ResultsFinder
*
* @description
*
*/
angular
.module('itemManager')
.service('ResultsFinder', ResultsFinder);
}());
As a side note, I added a static $inject
"annotation" to your class. This is a best practice if you are not using something like ngAnnotate. It also makes it easier to change out implementations by only changing the $inject
values.
If you are a ES5 developer it may help to think about how this would look in ES5
ResultsFinder.$inject = ['$http'];
var ResultsFinder = function($http) {
this.$http = $http;
}
ResultsFinder.prototype.getResults = function() {
return this.$http.get('/foo/bar/');
}
NOTE
Since you are using ES6, should probably use ES6 modules to organize your code.
You define and export your angular-module within an ES6 module:
import {module} from 'angular';
export var itemManager = module('itemManager', []);
Then import the Angular module
import {itemManager} from '../itemManager';
class ResultsFinder {
static $inject = ['$http'];
constructor($http) {
this.$http = $http;
}
getResults() {
return this.$http.get('/foo/bar/');
}
}
itemManager.service('ResultFinder', ResultFinder);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With