角度绑定与ngInfiniteScroll无法正常工作

时间:2016-05-29 14:37:59

标签: angularjs firebase nginfinitescroll

基本上我有一个时间表,帖子是$ firebaseArray,对这个数组的任何更改都会正确绑定。但是当我想绑定任何其他数据时,它只会在ngInfiniteScroll尝试从firebase中检索更多数据时绑定,所以只有当我向下滚动时才会绑定。

在下面的代码中,我正在调用{{getMoreDetails()}},当使用ngInfiniteScroll检索第一组数据时,此数据被绑定,但是一旦加载,绑定就会中断,并且只在滚动时再次绑定。 / p>

我的担忧是:

  • ngInfiniteScroll是否设计为以这种方式工作?
  • 此方案中是否有解决方法?

堆栈:

"firebase": "2.4.2","angularfire": "~1.2.0","firebase-util": "0.2.5","ngInfiniteScroll": "1.2.2"

timeline.html

<div ng-controller="TimelineController">
    <section class="entrys main-content" infinite-scroll="posts.scroll.next(3)" infinite-scroll-distance="0.3">
        <div class="inner">
            <div ng-repeat="post in filteredPostsResults = (posts | filter:postIdFilter)">
                <article class="entry">

                    <img ng-if="post.sourceType=='IMAGE'" data-ng-src="{{getPostData(post)}}"/>

                    <div class="entry-info">
                        <h3><div ng-bind-html="post.description | emoticons"></div></h3>
                        <small>posted on <time>{{getDateInFormat(post.createdAt)}}</time></small>
                        {{getMoreDetails()}}
                    </div>

                </article>
            </div>
        </div>
    </section>
</div>

timeline.js

(function (angular) {
      "use strict";

        var timeline = angular.module('myApp.user.timeline', ['firebase', 'firebase.utils', 'firebase.auth', 'ngRoute', 'myApp.user.timelineService']);

        timeline.controller('TimelineController', [ '$scope', '$routeParams', 'TimelineService', '$publisherServices', '$securityProperties', function ($scope, $routeParams, TimelineService, $publisherServices, $securityProperties) {

            if (!$scope.posts){
                $scope.posts = TimelineService.getPosts($routeParams.userId);
            }
            $scope.posts.$loaded(function(result) {
                $scope.isPostsLoaded = true;
            });


            $scope.getMoreDetails = function() {
                console.log("LOGGED ONLY WHEN SCROLLING");
                return $publisherServices.getDetails();
            };

            $scope.getPostData = function(post) {
                if (!post.dataUrl){
                    post.dataUrl = $publisherServices.getAwsFileUrl(post.fileName);
                }
                return post.dataUrl;
            };

            $scope.postIdFilter = function(post) {
                if ($routeParams.postId){
                    if (post.$id == $routeParams.postId) return post;
                } else { return post; }
            };

            $scope.getDateInFormat = function(timestamp){
                var date = new Date();
                date.setTime(timestamp);
                return date;
            };

        }]);

    })(angular);

timelineService.js

 (function (angular) {
      "use strict";

    var timelineService = angular.module('myApp.user.timelineService', []);

    timelineService.service('TimelineService', ['$routeParams', 'FBURL', '$firebaseArray', function ($routeParams, FBURL, $firebaseArray) {
        var posts;
        var currentUserIdPosts;
        var postsRef;

        var self = {
          getPosts: function(userId){
            if (!posts || userId != currentUserIdPosts){
              currentUserIdPosts = userId;
              postsRef = new Firebase(FBURL).child("posts").child(userId);
              var scrollRef = new Firebase.util.Scroll(postsRef, "createdAtDesc");
              posts = $firebaseArray(scrollRef);
              posts.scroll = scrollRef.scroll;
            }
            return posts;
          }

        }
        return self;
      }]);

    })(angular);

1 个答案:

答案 0 :(得分:3)

我假设您希望Firebase中的数据发生更改时更新帖子详细信息。

当Firebase更改应用于您的范围时,它似乎不会触发摘要周期,因此您可能需要在每次从Firebase获取更新时手动执行此操作。

$$updatedsee docs)中查看$firebaseArray.$extend

// now let's create a synchronized array factory that uses our Widget
app.factory("WidgetFactory", function($firebaseArray, Widget) {
  return $firebaseArray.$extend({

    // override the update behavior to call Widget.update()
    $$updated: function(snap) {
      // we need to return true/false here or $watch listeners will not get triggered
      // luckily, our Widget.prototype.update() method already returns a boolean if
      // anything has changed
      return this.$getRecord(snap.key()).update(snap);
    }
  });
});

我希望这会有所帮助。

相关问题