Question

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 ?

Was it helpful?

Solution

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
}

OTHER TIPS

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

Just do:

angular.module('myApp')

.controller('LoginController', function($q, $scope, $location, $routeParams, $window, authService, regService) {
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top