1

I have a service called authService. I call it from the Logincontroller as follows :

angular.module('myApp').controller('LoginController',function($q, $scope, $location,$routeParams, $window, authService) {//some code here});

this works fine.

Now I have another service called regService which is like :

angular.module('myApp').factory('regService ', function ($window, $q, $rootScope) {//some code here});

Now in LoginController how do I call functions from regService ?

2
  • 1
    Just pass regService to LoginController function like any other parameter. May 5, 2014 at 10:44
  • angular.module('myApp').controller('LoginController',function($q, $scope, $location,$routeParams, $window, authService, regService) { /* ... */});
    – MW.
    May 5, 2014 at 10:46

3 Answers 3

0

Both existing answers are technically correct, but if you decide to minify/uglify your code anytime in the future, those solutions will eventually break.

To prevent this, John Papas AngularJS style guide favors the use of the $inject service in Y091 like so :

angular.module('myApp').controller('LoginController', LoginCtrl);

LoginCtrl.$inject = ['$q', '$scope', '$location', '$routeParams', '$window', 'authService', 'regService'];

function LoginCtrl($q, $scope, $location, $routeParams, $window, authService, regService) {
  //some code here
}
0

Just do it like authService, you just need to add regService. AngularJS will automatically bind the service based on name. You might want to understand more about Dependency Injection - https://docs.angularjs.org/guide/di

angular.module('myApp').controller('LoginController',function($q, $scope, $location,$routeParams, $window, authService, regService) {//some code here});
0

Just do:

angular.module('myApp')

.controller('LoginController', function($q, $scope, $location, $routeParams, $window, authService, regService) {
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.