repeat处理从firebase推送的要显示的新帖子。我想显示新消息的通知,让用户点击一个按钮"显示新帖子",以显示新的重复项目。现在它会自动显示而不通知用户。是否有一种方法来处理这样的功能?非常感谢您的帮助
如果我理解了你的问题,这可能是处理Karthik的一种方法。
-
让你绑定到ng-repeat的数据拷贝来自service/api等
角。复制(dataForNgRepeat dataFromServer);
-
显示一个通知,如果dataFromServer发生变化。范围。看美元……等。
-
按钮逻辑需要通过再次从服务器复制数据来更新dataForNgRepeat。这样,它们就解耦了,但是你可以根据需要更新ng-repeat的数据。
很简单:)
例如,您可以使用$interval
定期查询DB中的新帖子,然后如果amount of posts from DB > amount of posts in $scope
控制器:
// Initial loading of posts
...
$scope.newPostsAvailable = false;
$interval(function() {
$http.get("/whatever/you/need")
.then(function(response) {
if (response.length > $scope.amountOfPosts) {
$scope.fetchedPosts = response;
$scope.newPostsAvailable = true;
};
})
}, 10000);
$scope.loadNewPosts = function() {
// load the new posts into your ng-repeat scope variable
$scope.newPostsAvailable = false;
}
HTML: <alert ng-show="newPostsAvailable" ng-click="loadNewPosts()" type="info">New posts available. Click to load...</alert>
<!-- Your ng-repeat here -->
这是旧的,但由于它没有作为一个选项:在ng-repeat中使用带有动态值的limitTo过滤器。
<div ng-repeat="item in items | limitTo: displayLimit">
{{item}}
</div>
<button
ng-if="items.length>displayLimit"
ng-click="displayLimit=items.length">
Show New Items
</button>
displayLimit最初设置为items.length。如果items获得新内容,因此displayLimit的值低于items。长度时,向用户显示一个按钮,该按钮将displayLimit变量增加为items.legth.