I am developing angularjs application. I have one javascript function to fill dropdown. I am making http call to get data from api. I have factory to return data. Below is code.
function fillperiod(fillperiodService) {
fillperiodService.getData().then(function(res) {
$scope.cal = response.data.data.Period;
});
}
myapp.factory('fillperiodService', ['$http', '$cookieStore', 'cfg', '$q', function($http, $cookieStore, cfg, $q) {
var baseurl = cfg.Baseurl;
var urlapi = baseurl + "api/Vehicle/GetEMIPeriod";
return {
getData: function() {
var q = $q.defer();
$http.get(urlapi).then(function(response) {
q.resolve(response);
}, function(error) {
q.reject();
})
return q.promise;
}
}
}]);
Above code throws error:
Cannot read property 'getData' of undefined.
I have declared method getData. I am not sure what i am missing in the above code. May i get some help to fix this issue? Any help would be appreciated. Thank you.
$qin your factory, just return$http.get()as that is already a promise.fillperiodin angular context from where you will passfillperiodServiceto it, or it need to be a controller/service wherefillperiodServiceis injectedfillperiodand from where?fillperiodServiceand have to pass that instance tofillperiod