2016-03-08 16 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)

相關問題