I am encountering difficulties when trying to access Angular's built-in services like $http while creating a service with ES6.
For instance, I am developing a "ResultsFinder" service that will make an AJAX call and then perform some operations. The issue arises when I realize that I can only utilize $http within the constructor (if passed as an argument), but not in other methods such as getResults.
Take a look at this code snippet:
(() => {
'use strict';
class ResultsFinder {
constructor($http) {}
getResults() {
return 'ResultsFinder';
}
}
/**
* @ngdoc service
* @name itemManager.service:ResultsFinder
*
* @description
*
*/
angular
.module('itemManager')
.service('ResultsFinder', ResultsFinder);
}());
In the getResults method, the $http is inaccessible. To gain access, I would need to resort to something that does not feel right like the following:
(() => {
'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);
}());
Can anyone provide me with advice on the correct approach to address this issue?