1

I am getting error in controller.js

controller.js

angular.module('myApp', ['ngRoute', 'ui.router']).controller('authCtl', ["$scope", "$rootScope", "loginService", function($scope, $rootScope, loginService) {
  $rootScope.bodyClass = "focusedform";
  $scope.submitForm = function() {
    loginService.userAuth($scope.username, $scope.password);
  }
}]);

factory.js

angular.module('myApp').factory('loginService', ["$http", "$q", function($http, $q) {
  userFactory = {};
  userAuth = function(username, password) {
    console.log(username + '' + password);
  };
  return true;
}]);

enter image description here

1
  • 1
    You're returning true from login service. Return an object or something with the userAuth function attached. Commented Mar 15, 2017 at 4:53

1 Answer 1

5

You didn't return your object in factory.

You have to attach your methods variable in an object and return it from factory in order to access from controller.

Try like this

angular.module('warApp').factory('loginService', ["$http", "$q", function($http, $q) {
  var userFactory = {};
  userFactory.userAuth = function(username, password) {
    console.log(username + '' + password);
  };
  return userFactory;
}]);

or try like this for better view

angular.module('warApp').factory('loginService', ["$http", "$q", function($http, $q) {
  var userFactory = {
    userAuth: userAuth
  }
  return userFactory;
  function userAuth(username, password) {
    console.log(username + '' + password);
  }
}]);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.