我想使用 ng-infinite-scroll ( https://sroze.github.io/ngInfiniteScroll/ )。但是当我想从我的服务调用函数(注入控制器)时,什么也没发生(这个函数不会触发)。当我不是从服务而是从控制器的 $scope 调用函数时 - 一切正常。如何从无限滚动指令中的注入服务调用函数?
我的 HTML 结构:
<div class="col-xs-12 publications-container" ng-controller="showPublicationsCtrl">
<h3 class="publ-heading">Proposed Publications</h3>
<ul class="list-group" infinite-scroll="publicationsFactory.getPublications()" infinite-scroll-disabled='publicationsFactory.busyLoadingData'>
<li ng-repeat="publication in publications" class="list-unstyled list-group-item" ng-cloak>
<p class="text-warning">Author: <i>{{publication.author}}, {{publication.timestamp | date: 'MMM. d, y'}}</i></p>
<p ng-text-truncate="publication.text" ng-tt-words-threshold="3">{{publication.text}}</p>
<p class="text-muted">Channels: <i>{{publication.channels.join(", ")}}</i></p>
</li>
</ul>
</div>
我的showPublicationsCtrl
控制器:
twitterApiApp.controller("showPublicationsCtrl", ['publicationsFactory', '$scope', function (publicationsFactory, $scope) {
publicationsFactory.getPublications();
$scope.publications = publicationsFactory.publications;
}]);
我的publicationsFactory
服务:
angular.module('twitterApiApp').factory('publicationsFactory', ['$http', function($http) {
var publicationsFact = {};
publicationsFact.publications = [];
publicationsFact.busyLoadingData = false;
publicationsFact.getId = function() {
publicationsFact.id = publicationsFact.publications.length > 0 ?
publicationsFact.publications[publicationsFact.publications.length-1]['id'] : 0;
};
publicationsFact.getPublications = function () {
console.log("Triggered");
if (publicationsFact.busyLoadingData) return;
publicationsFact.busyLoadingData = true;
publicationsFact.getId();
$http.get("/Publications?id_gte=" + publicationsFact.id + "&_limit=2").then(function(response) {
for (var i = 0; i < response.data.length; i++) {
publicationsFact.publications.push(response.data[i]);
};
});
publicationsFact.busyLoadingData = false;
};
return publicationsFact;
}]);
例如,如果我在我的控制器中创建一些函数,$scope.myFunction
然后在 HTML 结构中我为该函数分配infinite-scroll
属性myFunction()
将成功执行。所以,我认为我在控制器中注入服务的方式可能存在一些错误。但是除了 ng-inginite-scroll 之外的所有其他东西都按计划工作。