2

You have 2 angular modules, and one of those modules has a service that you want to use in the other module:

var moduleA = angular.module ('moduleA', [])
                 .service('FirstService', function(){
                    var thing = {};
                    thing.data = ['1', 'alskdjf' , 0];
                    return thing;
                 });

var moduleB = angular.module('moduleB', []);

The question is- how do you use 'FirstService', from within moduleB?

This fails:

moduleB.controller('MBC', ['$scope', 'moduleA', function($scope, moduleA){}])

These fail too:

moduleB.controller('MBC', ['$scope', function($scope){
  var a = angular.injector().get('FirstService');
  var b = angular.injector().invoke('FirstService');
  var c = moduleA.service('FirstService');
  var d = moduleA.FirstService;

}])
3
  • 1
    I think you need to inject module A into module B like this var moduleB = angular.module('moduleB', ['moduleA]); Commented Apr 13, 2015 at 4:33
  • yep. I knew it was something easy like that. if you create that as an answer, you'll get rep Commented Apr 13, 2015 at 4:46
  • 1
    I have answered it. You can accept my answer, if it has helped you Commented Apr 13, 2015 at 5:12

3 Answers 3

3
var moduleB = angular.module('moduleB', ['moduleA]);

This will inject moduleA into moduleB , and then you can use moduleA service.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is simple Example to use angular service.

mainApp.service('CalcService', function(MathService){
    this.square = function(a) {
        return MathService.multiply(a,a);
    }
});
mainApp.controller('CalcController', function($scope, CalcService) {
    $scope.square = function() {
       $scope.result = CalcService.square($scope.number);
    }
});

1 Comment

This doesn't solve the question... or really even address it. You're injecting MathService, but not showing where / which app it's registered to and also relying on implicit injection rather than explicit... meaning you're not using array or $injector syntax which is error prone and harder to debug.
0

I wrote an answer in Plunkr that works. Long story short, you add your second module as a dependency into your first one.

From there you can inject any services attached to the second module.

You don't need to use the .injector() function at all. (that's not the recommended way either).

Go to this link: http://plnkr.co/edit/7Q6dvtpvFJforeJhTLCR?p=info

var app = angular.module('plunker', ['separateModule']);

app.controller('MainCtrl', function($scope, TestService) {
  $scope.title = 'Plunker Example for Stack Overflow';
  $scope.message = TestService.sayHi();
});

//Second module
var anotherModule = angular.module('separateModule', []);

//Let's declare of Service in the Second module
anotherModule.service('TestService', function() {
    this.message = 'Hello Stack Overflow';
    
    this.sayHi = function() {
      return this.message;
    };
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <h1>{{ title }}</h1>
    <p>
      {{ message }}
    </p>
  </body>

</html>

1 Comment

@andrew-luring did this help?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.