문제

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 ?

도움이 되었습니까?

해결책

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
}

다른 팁

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) {
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top