angularjs - angular how to pass an object from controller to service -


i have in controller retrieve , object inside $scope.formdata.

angular.module('starter.controllers', [])  .controller('logincontroller', function($scope, authentication){ $scope.formdata = {};  $scope.processform = function(){     authentication.login($scope.formdata);     //$http({     //    method  : 'get',     //    url     : 'http://localhost:8888/employees/login',     //    params: $scope.formdata     //})     //    .success(function(respond){     //        $scope.state.go('main');     //    }) }  }) 

in service js have method called login suppose retrive data sent controller, far im getting value null when console.log(formdata),.

angular.module('starter.services', [])  .factory('authentication', function($http){      this.login = function(formdata){     console.log(formdata);     return $http({             method  : 'get',             url     : 'http://localhost:8888/employees/login',             params: formdata         }) }      return null; }) 

this html

<ion-view view-title="login"> <ion-content class="padding has-header">     <div class="row">         <div class="col col-50 col-offset-25">             <div class="card">                 <div class="item item-text-wrap">                     <form ng-submit="processform()">                         <div class="list">                             <label class="item item-input">                                 <input type="text" placeholder="first name" ng-model="formdata.accesskey" required="true">                             </label>                             <label class="item item-input">                                 <input type="text" placeholder="last name" ng-model="formdata.password" required="true">                             </label>                         </div>                         <button class="button button-full button-positive">                             sign me in                         </button>                     </form>                 </div>             </div>         </div>     </div> </ion-content> 

i console error cannot read property 'login' of null

my app.js

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services']) 

if read code correctly, problem using 2 different modules.

try use same modules controller

 angular.module('samemodule', [])      .controller('logincontroller', function($scope, authentication){ ...} 

and service/factory like

     angular.module('samemodule', [])  .factory('authentication', function($http){... } 

or inject module service/factory module of controller:

angular.module('starter.controllers', ['starter.services']) 

read topic, understand idea...

hope helpfull.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -