how to call a function from module dependency (another module)

前端 未结 3 1113
不思量自难忘°
不思量自难忘° 2021-01-21 16:57

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 m

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-21 17:22

    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

提交回复
热议问题