2016-03-08 25 views
1

我在使用helper函数设置的上下文变量的指令中使用ng-repeat,但该指令并未呈现整个集合。据我所知,它正在渲染在指令postLink时发现的任何内容,但在收集增长时不会重新呈现。angular-meteor:你是如何看待`helpers`方法的集合?

我试图给指令添加一个$watch,但它只在加载时触发一次,即使Collection增长。

下面是我从meteor-angular-socially教程创建了一个示例,请参阅:https://github.com/Urigo/meteor-angular-socially/tree/step_14

文件:client/parties/parties-list/parties-list.component.js

angular.module('socially').directive('partiesList', function() { 
    return { 
    restrict: 'E', 
    templateUrl: 'client/parties/parties-list/parties-list.html', 
    controllerAs: 'partiesList', 
    controller: function ($scope, $reactive) { 
     $reactive(this).attach($scope); 

     window.vm = vm = this; 
     $scope.$watch(vm.parties, function(newV, oldV) { 
     // this $watch fires once when the controller is loaded, but never again 
     // but vm.parties is updated when new parties are added 
     return console.warn('vm.parties.count=' + (newV != null ? newV.length : void 0)); 
     }); 

     ... 

     this.helpers({ 
     parties:() => { 
      return Parties.find({}, { sort : this.getReactively('sort') }); 
     }, 
     users:() => { 
      return Meteor.users.find({}); 
     }, 
     partiesCount:() => { 
      return Counts.get('numberOfParties'); 
     } 
     }); 

集合时被更新的$scope.$watch不火。我在这里做错了什么?

回答

0

dooh!答案是使用scope.$watchCollection,而不是scope.$watch

0

@迈克尔scope.$watchCollection可能在某些情况下工作,但如果你想观看的所有数据变化,你需要使用深表:$ $范围腕表(FUNC,真正

$scope.$watch(vm.parties, function(newV, oldV) { 
    return console.warn('vm.parties.count=' + (newV != null ? newV.length : void 0)); 
}, true); 

而且getReactively可以使用深手表太多,如果你设置第二个参数为true:$scope.getReactively('var', true)