2017-03-21 12 views
1

Ich benutze angularjs 1.5.8, die ich craete Komponentenname als Gruppe mit Gruppenmodul, ich erstelle Direktive des gleichen Moduls als Komponente namens blättern, Ich bin in erfordern Um die Komponentenfunktion aufzurufen, die loadmoregroups ist, um api dort aufzurufen, um beim Scrollen mehr Gruppen am Bootom zu erhalten.Wie winkeln Komponente Funktion von außen Direktiven

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

Die beste Vorgehensweise ist mit rootScope mit Funktionen zu vermeiden. Sehen Sie mehr hier: http://Stackoverflow.com/questions/32761540/why-using-rootscope-with-functions-is-not-recommended –

Antwort

2

Ich weiß nicht, ob dies der beste Weg ist, aber Sie können Broadcast verwenden, um Elternkomponente aus Kindkomponente aufzurufen.

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

Komponente

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

danke @sachila das hatte Arbeit für mein Ziel – Rahil

+0

schön stellen Sie sicher, als richtige Antwort zu markieren Wenn das half: D –

Verwandte Themen