0

i have this in my controller that retrieve and object inside the $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 the service js i have a method called login that it suppose to retrive the data sent from the controller, but so far im getting a value null when i do 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 is my 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 get this console error Cannot read property 'login' of null

my app.js

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
5
  • Can you provide your html code? Commented Jul 2, 2015 at 1:06
  • Did you try passing the formData to processForm()? E.g. processForm(formData) then assigning it in the controller? Commented Jul 2, 2015 at 1:12
  • 1
    I haven't used angular in a long time, but shouldn't your factory return this? Commented Jul 2, 2015 at 1:16
  • i get this console error Cannot read property 'login' of null Commented Jul 2, 2015 at 1:28
  • That's an interesting pattern I hadn't seen before - create all your controllers in 'module.controllers' and services in 'module.services'. Then create a 'module' module that takes both sub-modules as dependencies... Commented Jul 2, 2015 at 1:49

2 Answers 2

0

If I read your code correctly, the problem is that you are using 2 different modules.

Try to use same modules for controller

 angular.module('sameModule', [])

    .controller('loginController', function($scope, Authentication){
...}

and service/factory like

     angular.module('sameModule', [])

.factory('Authentication', function($http){...
}

or to inject module from service/factory to module of controller:

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

Read also this topic, it could help you to understand the idea...

Hope it will be helpfull.

Sign up to request clarification or add additional context in comments.

2 Comments

in my app.js im including both modules
Try to use same modules for controllers and factory.
0

i found my issue

.factory('Authentication', function($http){

return {
    login:  function(formData) {
        $http({
            method  : 'GET',
            url     : 'http://localhost:8888/employees/login',
            params: formData
        })
        .success(function(respond){
            console.log(respond);
        })
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.