How to call a function from another module in Angular Js


Refactor your code in following these steps:

  1. Define a service in module A
  2. Add module A as dependency to module B
  3. Inject the service into the controller

Here is an example:

angular.module('A').service('API', function ($http) {
    this.getData = function () { return $http.get('/data'); };
});

angular.module('B', ['A']).controller('dashboardCtrl', function ($scope, API) {
    API.getData().then(function (data) { $scope.data = data; });
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s