我正在使用angularjs 1.5.8,我使用组模块将组件名称作为Group,同样我创建与名为scroll的组件相同模块的指令, 我需要调用loadmoregroups的组件函数来调用api,以便在bootom上滚动更多组。
angular.module('group', []).
component('group', {
templateUrl: '/djangotemplates/private/group/list.html',
controller: function(
$cookies,
$http,
$location,
$scope,
Flash,
$animate,
Group,
$timeout,
$rootScope
){
$rootScope.loadMoreGroups =function()
{
alert("loadmore");
}
}
}).directive('scroll', function() {
return {
restrict: 'A',
link: function(rootScope, element, attrs, $window, $scope, $document) {
var bind = element.bind('tbody');
var raw = element[0];
angular.element(bind).on("scroll", function() {
//console.log('in scroll mode');
if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) {
**rootScope.loadMoreGroups();** //unable to call this `enter code here`function
}
});
}
};
});
答案 0 :(得分:2)
我不知道这是否是最佳方法,但您可以使用广播从子组件调用父组件。
if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) {
$scope.$broadcast("call_func")
}
成分</ P>
.component('group', {
templateUrl: '/djangotemplates/private/group/list.html',
controller: function(
$cookies,
$http,
$location,
$scope,
Flash,
$animate,
Group,
$timeout,
$rootScope
) {
$rootScope.loadMoreGroups = function() {
alert("loadmore");
}
$scope.$on("call_func", function(ev) {
$rootScope.loadMoreGroups()
})
}
})