Reputation: 3
I am new to angular. I have a service where I call a function by another function within the service. But in my controller is shows as undefined. See below
//my Service
myApp.service("LocationService", function() {
var srv = {}
srv.msg = {long:0, lat:0};
srv.onSuccess = function() {
this.msg.lat = 32;
},
srv.onError = function() {
this.msg.long = 99;
},
srv.getGpsFix = function() {
this.onSuccess();//fails in controller
}
return srv;
});
//my Controller
myApp.controller("MusicCtrl", ["$scope", "LocationService", function($scope, LocationService) {
//undefined
console.log(locationService.getGpsFix());
}]);
Upvotes: 0
Views: 499
Reputation: 89
locationService.getGpsFix()
is undefined
. In your controller, your service is available as LocationService
. Hence, use LocationService.getGpsFix()
Upvotes: 1
Reputation: 9466
It is correct that locationService.getGpsFix()
would return undefined
.
If you intended for it to return a value, then use the return
keyword in your function.
srv.getGpsFix = function() {
this.onSuccess();
return 'It worked!!!';
};
Upvotes: 1