Prakhar Maheshwari
Prakhar Maheshwari

Reputation: 21

Angular JS - is not a function

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

Upvotes: 1

Views: 83

Answers (1)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

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);
  }
}]);

Upvotes: 5

Related Questions