Skip to content Skip to sidebar Skip to footer

How To Call A Function From Module Dependency (another Module)

I want to create seperate module for pagination, because I will need it to reuse in different modules, but I don't know how to call a function from module dependency (another modul

Solution 1:

To share resources across two modules, declare a factory or a service.

Suppose you have a search module:

//you inject your pagination module herevar searchModule = angular.module('search', ['pagination']);

//and you ALSO need to inject your pagination's service into your controller
searchModule.controller('MainController', ['$scope', 'paginationSvc'function($scope, paginationSvc) {
  //function from pagination module controller
  paginationSvc.testFunction();
}])

And your pagination module:

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

//declare your pagination service here
pagination.factory('PaginationSvc', [function(){
 return {
   testFunction:testFunction
 };
 functiontestFunction(){
   console.log('This is from pagination module');
}
}])

Maybe a little plnkr will help :D

Solution 2:

You can't inject a controller in a controller. Your pagination module need to declare a service or factory or provider wich will be be injected in your controller.

See docs here.

Solution 3:

You should create a pagination directive and insert that where you need it. You might get some good ideas from this post.

Post a Comment for "How To Call A Function From Module Dependency (another Module)"