JackTheKnife JackTheKnife - 2 months ago 15
AngularJS Question

AngularJS - access factory service from the controller

I'm trying to access

factory
service within
controller
to obtain correct data.

Related controller code looks like:

myApp.controller('RegistrationController', ['$scope','$routeParams','$rootScope','$location','$filter','$mdDialog','checkAttendee', function($scope, $routeParams, $rootScope, $location, $filter, $mdDialog,checkAttendee){

...

$scope.addAttendee = function(ev) {
$mdDialog.show({
controller: AddDialogCntrl,
templateUrl: 'views/regForm.tmpl.html',
parent: angular.element(document.body),
targetEvent: ev,
clickOutsideToClose:true,
controllerAs: 'ctrl',
fullscreen: $scope.customFullscreen, // Only for -xs, -sm breakpoints.
locals: {parent: $scope}
})
.then(
function(response){
if(angular.isDefined(response)){
attendees.push(response);
checkAttendee.getAttendeeInfo(response);
}
},
function(){
//no changes
}
)
.catch(
function(error) {
console.log('Error: ' + error);
}
)
};


and
factory
service code

myApp.factory('checkAttendee', ['$http', function($http) {

this.getAttendeeInfo = function(req) {

return $http.get("/check/attendee/", {params:{"firstName":req.firstName, "lastName":req.lastName, "email": req.email, "eventID": req.eventID}})
.then(function(response) {
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;

console.log('Data: ' + data);
console.log('Status: ' + status);

return data;
})
.catch(function(response) {
console.log('something worng');
});
}
}]);


but that combination gives me an error
Provider 'checkAttendee' must return a value from $get factory method.
when there is a return value.

Any thoughts?

Answer Source

Option 1

When we work with factories the structure should be:

myApp.factory('checkAttendee', ['$http', function($http) {

var factory = {

       getAttendeeInfo : function () {                
            return $http.get(/**/).then(function(response) {
                  // ..
                   return data;   
            }                         
        }
   }       
    return factory;
}]);

DEMO 1

Option 2

you can change factory to service and everything should work. A.e.:

myApp.service('checkAttendee', ['$http', function($http) {

    this.getAttendeeInfo = function(req) {

        return $http.get("/check/attendee/",  {params:{"firstName":req.firstName, "lastName":req.lastName, "email": req.email, "eventID": req.eventID}})
            .then(function(response) {
                var data = response.data;
                var status = response.status;
                var statusText = response.statusText;
                var headers = response.headers;
                var config = response.config;

                console.log('Data: ' + data);  
                console.log('Status: ' + status);

                return data;    
            })
            .catch(function(response) {
                console.log('something worng');
            });
    }
}]);

DEMO 2


Keep in mind that service extends the factory