CMckinstry CMckinstry - 4 years ago 119
AngularJS Question

Angular Service returning "undefined"

So I am sure I am not using best practices, but, I'm just trying to get this to work. I'm making a note taking app, and for whatever reason, the service I created, returns

undefined
and I can't figure out why.

Here's the service:

angular.module('notesService', []).factory('Notes', ['$http', function($http){
return {
get : function(){
var notes = $http.get('/api/notes');
return notes;
}
}
}]);


And here is the controller:

angular.module('mainController', [])
.controller('mainController', function($scope, Notes){
console.log(Notes.get());
});


The controller is not producing anything on the page just yet, i'm still testing.

Here is what the service returns to my controller:

e {
$$state : {
status : 1,
value : {
config : Object,
data: Array[10]
}
}
}


This isn't the entire thing, but it is all the stuff I need for my purposes.

Whenever I access
$$state.value
it returns
undefined
and I have no idea why.

Answer Source

You have the service in an entirely different module. So you gotta inject notesService into angular.module('mainController', [notesService]).

You dont ideally need to add new module for each controller and services, you can have single module and add everything to it

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download