Rahil
Rahil

Reputation: 632

How to call angular component function from out side directives

I am using angularjs 1.5.8 , Which I craete component name as Group using group module , same i create directive of same module as component named as scroll , I am in require to call the component function that is loadmoregroups to call api there to get more groups on scroll, at the 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

                }
            });
        }
    };
});

Upvotes: 1

Views: 894

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41445

i don't know whether this is the best way but you can use broadcast to call parent component from child component.

 if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) {
     $scope.$broadcast("call_func")
 }

component

.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()
        })
    }
})

Upvotes: 2

Related Questions