【问题标题】:How to filter subscribed data Angular-Meteor way如何过滤订阅的数据Angular-Meteor方式
【发布时间】:2015-06-06 20:18:29
【问题描述】:

假设我们需要在视图中显示过滤后的任务,第一步我们获得了购买发布/订阅的所有任务列表:

发布:

Meteor.publish("tasks", function(options){
    return Tasks.find(options);
});

订阅:

var allTasks = $meteor.collection(Tasks).subscribe('tasks', {});

现在,假设我只需要在 $scope 中查看将“活动”变量设置为 true 的任务。

类似这样的:

$scope.active_tasks = getFilteredTasks(allTasks, {active: true}) 

如何替换 getFilteredTasks(allTask​​s, {active: true}) 以获取只有 active==true 变量的任务?

我知道我们可以在订阅以下任务时设置“选项”:

{active: true}

但这无助于解决问题。目标是只订阅一次,然后使用过滤器只显示部分任务。

Angular-Meteor tutorial我还没有写完,所以后面可能会解释,如果你能指出正确的教程,它将大大加快我的学习速度。

提前致谢。

【问题讨论】:

    标签: angularjs meteor angular-meteor


    【解决方案1】:

    好的。看起来解决方案在 Angular 方面。 所以在控制器中我们得到了所有的任务:

    $scope.active_tasks = $meteor.collection(Tasks);
    

    在视图方面我们过滤它们:

    <input ng-model="freeText">
    <tr ng-repeat="task in active_tasks | filter:freeText ">
        <td>{{ task }}</td>
    </tr>
    

    :)

    【讨论】:

      【解决方案2】:

      @pumych 解决方案是一个有效的解决方案。 你也可以使用

      $scope.$meteorSubscribe ('tasks'); 
      $scope.activeTasks = $meteor.collection (function (){ 
      // active tasks will only be the filtered result
      return Tasks.find (active: true) ; 
      }); 
      

      在此处https://medium.com/@tally_b/coll-pub-sub-with-angular-meteor-cb13fe48f5701 了解 Angular Meteor 中的数据流。查看示例和 github 代码了解更多详情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 1970-01-01
        • 2015-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多