AngularJS - access factory service from the controller -


i'm trying access factory service within controller 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, // -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 combination gives me error provider 'checkattendee' must return value $get factory method. when there return value.

any thoughts?

option 1

when work factories 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 service , 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');                 throw response;             });     } }]); 

demo 2


keep in mind service extends factory


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -