如何在angularjs的controller中传递多个服务?

如何在angularjs的controller中传递多个服务?,angularjs,dependency-injection,angular-services,Angularjs,Dependency Injection,Angular Services,我有一个叫做authService的服务。我从Logincontroller中调用它,如下所示: angular.module('myApp').controller('LoginController',function($q, $scope, $location,$routeParams, $window, authService) {//some code here}); 这个很好用 现在我有了另一个名为regService的服务,它类似于: angular.module('myApp')

我有一个叫做authService的服务。我从Logincontroller中调用它,如下所示:

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

现在我有了另一个名为regService的服务,它类似于:

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

现在,在LoginController中,如何从regService调用函数?

只需像authService一样执行,您只需添加regService。AngularJS将根据名称自动绑定服务。您可能想进一步了解依赖项注入-

只要做:

angular.module('myApp')

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

现有的两个答案在技术上都是正确的,但如果您决定在将来的任何时候缩小/丑化代码,这些解决方案最终都会失败

为了防止出现这种情况,John Papas AngularJS style guide支持在like so中使用
$inject
服务:

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
}

只需像任何其他参数一样将
regService
传递给
LoginController
函数。
angular.module('myApp').controller('LoginController',function($q、$scope、$location、$routeParams、$window、authService、regService){/*…*/})
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
}