2

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 module)

Here is my main module:

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

App.controller('MainController', ['$scope', '$pagination' function($scope, $pagination) {
    $scope.testFunction(); //function from pagination module controller
}])

Here is my pagination module:

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

App.controller('PaginationController', ['$scope', function($scope) {
    $scope.testFunction = function(){
        console.log("pagination module");
    }
}])

I get error:

Error: [$injector:unpr] Unknown provider: $paginationProvider <- $pagination
3
  • 1
    You haven't declared a service (provider) called $pagination. Where is that? You can't inject something that doesn't exist. Why are you creating a controller in the pagination module, only to be injected into another controller? Commented Oct 3, 2014 at 7:40
  • could you post some example how to do it? Commented Oct 3, 2014 at 7:42
  • There's plenty here: docs.angularjs.org/guide/services . Instead of creating a controller in the pagination module, create a service/factory (and name it something else...Angular modules usually start with "$" and it's advised not to do it yourself Commented Oct 3, 2014 at 7:44

3 Answers 3

1

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

Suppose you have a search module:

//you inject your pagination module here
var 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
 };
 function testFunction(){
   console.log('This is from pagination module');
}
}])

Maybe a little plnkr will help :D

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

Comments

0

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.

Comments

0

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

Comments

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.