I am trying to create a service, and this is it:
(function () {
'use strict';
angular
.module('app')
.factory('appService', Service);
function Service($http) {
function getData() {
var request = new XMLHttpRequest();
request.open('GET', 'mock.json', false);
request.send(null);
return [request.status, request.response, {}];
}
return {};
}
})();
Where mock.json is in the same folder.
Then I try to call it from a controller:
(function () {
'use strict';
angular.module('app')
.controller('appCtrl', appCtrl);
/** @ngInject */
function appCtrl(appService) {
console.log(appService.getData());
}
})();
But it gives me an error:
TypeError: appService.getData is not a function
What am I doing wrong?
asked Dec 21, 2016 at 12:22
FacundoGFlores
8,13812 gold badges68 silver badges97 bronze badges
2 Answers 2
You are returning empty object, thus you are getting the expected error.
function Service($http) {
function getData() {
var request = new XMLHttpRequest();
request.open('GET', 'mock.json', false);
request.send(null);
return [request.status, request.response, {}];
}
//Here return the function reference of getData
return {
getData : getData
};
}
answered Dec 21, 2016 at 12:24
Satpal
134k13 gold badges168 silver badges171 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You are just missing to return the object with the proper properties on your Service
return {
getData: getData
}
answered Dec 21, 2016 at 12:26
taguenizy
2,2851 gold badge12 silver badges26 bronze badges
Comments
Explore related questions
See similar questions with these tags.
lang-js