【问题标题】:Is there a way to load ng-repeat new items with a click handler?有没有办法使用点击处理程序加载 ng-repeat 新项目?
【发布时间】:2016-09-26 18:20:19
【问题描述】:

重复处理从 firebase 推送的要显示的新帖子。我想显示有关新消息的通知,并让用户单击“显示新帖子”按钮以显示新的 ng-repeat 项目。现在它会自动显示它而不通知用户。有没有办法处理这样的功能? 非常感谢您的帮助

【问题讨论】:

  • 你能给我们看看你们的一些代码吗???

标签: angularjs firebase


【解决方案1】:

如果我了解您的问题,这可能是处理此 Karthik 的一种方法。

  1. 使您绑定到 ng-repeat 的数据成为来自服务/api 等的数据的副本。

    angular.copy(dataForNgRepeat, dataFromServer);

  2. 若要显示通知,请在 dataFromServer 更改时执行此操作。 $scope.$watch...等等。

  3. 按钮逻辑需要通过再次从服务器复制数据来更新dataForNgRepeat。这样,它们就解耦了,但是您可以根据需要更新 ng-repeat 的数据。

足够简单:)

【讨论】:

    【解决方案2】:

    例如,您可以使用$interval 定期查询数据库中的新帖子,如果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 -->
    

    【讨论】:

      【解决方案3】:

      这是旧的,但由于它没有作为选项提供: 在 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.length,则向用户显示一个按钮,这会增加 displayLimit 变量为 items.legth。

      【讨论】:

        猜你喜欢
        • 2017-07-09
        • 2011-06-26
        • 1970-01-01
        • 1970-01-01
        • 2016-03-09
        • 1970-01-01
        • 2018-04-15
        • 2020-04-10
        • 2022-01-04
        相关资源
        最近更新 更多