olivier olivier - 4 months ago 17
AngularJS Question

Use third party library in a angular factory

This is my factory:

.factory('DataService',['$http',function($http, Backand ){
var data = [];
return {
GetData: function(){
return $http.get(Backand.getApiUrl() + '/1/objects/details').then(function(response){
data = response;
return response;
});
},
GetElem: function(personId){
for(i=0;i<data.length;i++){
if(data[i].id == personId){
return data[i];
}
}
}
}

}]);


Now the problem. The third party library
Backand
is correctly injected but i get the error:
Cannot read property 'getApiUrl' of undefined


So Backand is undefined. When i use Backand in a controller everything work just fine.

What should i do to fix this?

Answer

You are missing Backand while injecting

Change this:

.factory('DataService',['$http',function($http, Backand ){

To This:

.factory('DataService',['$http', 'Backand',function($http, Backand ){

Hope this will work for you.