javascript-如何从模块依赖项(另一个模块)调用函数
作者:互联网
我想创建单独的模块进行分页,因为我需要在不同的模块中重用它,但是我不知道如何从模块依赖项(另一个模块)中调用函数
这是我的主要模块:
var App = angular.module('search', ['pagination']);
App.controller('MainController', ['$scope', '$pagination' function($scope, $pagination) {
$scope.testFunction(); //function from pagination module controller
}])
这是我的分页模块:
var App = angular.module('pagination', []);
App.controller('PaginationController', ['$scope', function($scope) {
$scope.testFunction = function(){
console.log("pagination module");
}
}])
我得到错误:
Error: [$injector:unpr] Unknown provider: $paginationProvider <- $pagination
解决方法:
要在两个模块之间共享资源,请声明工厂或服务.
假设您有一个搜索模块:
//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();
}])
和您的分页模块:
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');
}
}])
也许little plnkr会有所帮助:D
标签:javascript,angularjs,angularjs-scope 来源: https://codeday.me/bug/20191011/1892034.html