【发布时间】:2016-09-28 05:47:06
【问题描述】:
我正在使用 ionic 开发一个新闻应用程序。对于不同类别的新闻,我使用相同的模板文件和控制器功能。我的控制器功能如下。我正在使用 ion-infinite-loop 来检索旧消息,我的回调函数是 loadMore()。第一次调用一个类别时,我的代码工作正常,但是当我同时调用其他类别时 loadMore() 连续调用,作为无限循环。
.controller('categoryCtrl', ['$scope', '$http', '$stateParams', '$location', '$state',
function ($scope, $http, $stateParams, $location, $state, $ionicHistory) {
$scope.category = $location.path().split('/')[2];
$scope.articles = [];
$scope.last_id = 0;
$http({
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {'path': $location.path(), 'id': ''},
method: 'POST',
url: 'http://www.example.com/article/mobile_api/category',
}).success(function (newsData) {
angular.forEach(newsData, function (newsArticle) {
$scope.articles.push(newsArticle);
$scope.last_id = newsArticle.article_id;
});
}).error(function (data) {
alert('Warning! Home Page Request failed');
});
$scope.loadMore = function () {
$http({
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {'path': $location.path(), 'id': $scope.last_id},
method: 'POST',
url: 'http://www.example.com/article/mobile_api/category',
}).success(function (newsData) {
angular.forEach(newsData, function (newsArticle) {
$scope.articles.push(newsArticle);
$scope.last_id = newsArticle.article_id;
});
$scope.$broadcast('scroll.infiniteScrollComplete');
});
};
}]);
下面给出的 HTML 代码
<ion-content class="has-header bar-positive" padding="true">
<div class="list" ng-repeat="item in articles" style="margin-bottom: 5px;">
<a ng-click="">
<div class="row" >
<div class="col col-25"> <img width="100%" src="{{pic_path}}{{item.pic_name}}"></div>
<div class="col col-75 "><p class="item-body">{{item.article_name}}</p>
</div>
</a>
</div>
<ion-infinite-scroll
on-infinite="loadMore()"
distance="1%">
</ion-infinite-scroll>
</ion-content>
这是我的 route.js
angular.module('app.routes', [])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('example.technology', {
url: '/technology',
views: {
'side-menu21': {
templateUrl: 'templates/category.html',
controller: 'categoryCtrl'
}
}
})
.state('example.wolrd', {
url: '/world',
views: {
'side-menu21': {
templateUrl: 'templates/category.html',
controller: 'categoryCtrl'
}
}
})
$urlRouterProvider.otherwise('/side-menu21/home')
});
【问题讨论】:
-
一个问题:你为什么放 $scope.$broadcast('scroll.infiniteScrollComplete');在 $Scope.$apply 中?
-
@Naitik,对不起,我测试了一个案例,它被删除了
-
你删除后试过了吗?也请添加html代码
-
@Naitik,我已经测试过存在问题。我也添加了 HTML 代码
-
请检查我的回答,我就是这样做的,希望对你有帮助